2017-12-17 3 views
1

私は現在、半Uber、半Tinderのような機能を持つアプリケーションを開発しています。1つのテーブルビューでさまざまな種類のセルを作成する方法は?

プロフィールを編集するためのTableViewを作成して、Tinderのプロファイル編集ビューのように見せたいと思います。

私は正常にプロファイル画像を取得するための最初のセルを作成し、textLabelのために2番目のセルを作成しました。

編集可能なTextView(テキストフィールドではなく、複数行の単語を入力できるようにする)を持つ3番目のセルを作成したいのですが、意図したとおりにコードが機能しません。

カスタムセルクラスでtextViewを作成し、それを私のtableViewControllerクラスの3番目のtableViewセルに設定しても、シミュレータには表示されないようです。

私はまだ、単一のtableViewのためにいくつかのタイプのセルを作成するための良い方法は何もわかりません。それを行う他のより良い方法があるなら、私は知りたいです。

この問題に関するマイコードは以下のとおりです。

のViewController

//EditProfileViewController For The Profile Edit view 
class EditProfileViewController: UINavigationController { 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Do any additional setup after loading the view. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

} 

TableViewController

//MyTableViewController for the EditProfileViewController 
class EditProfileTableViewController: UITableViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UITextViewDelegate { 
override func viewDidLoad() { 
    super.viewDidLoad() 

} 

    override func numberOfSections(in tableView: UITableView) -> Int { 

    return 3 
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if section == 0 { 
     return 5 
    } else if section == 1 { 
     return 2 
    } else { 
     return 4 
    } 
} 


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    //link my custom Cell here 
    if let cell = tableView.dequeueReusableCell(withIdentifier: "MyCustomCell", for: indexPath) as? MyCellTableViewCell { 

     if indexPath.section == 0 { 

      if indexPath.row == 0 { 
       //this row is for an Image Picker 
       cell.textLabel?.text = "edit profile image" 
      } else if indexPath.row == 1 { 
       //this row is just like a title of third cell which is editable self description 
       cell.textLabel?.text = "Describe yourself" 
       cell.isUserInteractionEnabled = false 
      } else if indexPath.row == 2 { 
       //this row is an editable self description 
       cell.addSubview(cell.selfDescritionTextView) 
       cell.selfDescritionTextView.delegate = self 
       cell.selfDescritionTextView.isEditable = true 
       cell.selfDescritionTextView.text = "Enter some descriptions here" 


      } 

     } 

     else if indexPath.section == 1 { 
      cell.textLabel?.text = "section 1" 


     } 

     else if indexPath.section == 2 { 
      cell.textLabel?.text = "section 2" 

     } 


     return cell 
    } else { 
     //Just in case 
     return UITableViewCell() 
    } 
} 

} 

TableViewCell

//MyCellTableViewCell 
class MyCellTableViewCell: UITableViewCell { 

//create a textView for self description 
var selfDescritionTextView = UITextView() 

//init 
override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
    super.init(style: .default, reuseIdentifier: "MyCustomCell") 
    selfDescritionTextView = UITextView(frame: self.frame) 
    self.addSubview(selfDescritionTextView) 
} 

required init?(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 
} 


override func awakeFromNib() { 
    super.awakeFromNib() 
    // Initialization code 
} 

override func setSelected(_ selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 

    // Configure the view for the selected state 
} 

} 

ここに示したコードよりも多くのコードを知りたい場合は、教えてください。

私は私の編集プロフィールを作成するためにこの方法に夢中になっていません。誰かこの他の方法を知っていれば、私も知りたいです。

おかげで、このような

+0

すべてのセルがテーブルビューに埋め込まれていますか?すべてのセルにカスタムクラスがありますか? –

+0

ブレークポイントを使用し、条件が満たされているかどうかコントロールがcellForRowを超えているかどうかを確認できますか?if cell = tableView.dequeueReusableCell(withIdentifier: "MyCustomCell"、for:indexPath) MyCellTableViewCell – MBN

+0

そして、MyCellTableViewCellのストーリーボードまたはカスタムセルxibのtableviewプロトタイプセルを使用していますか? – MBN

答えて

1
override func viewDidLoad() { 
    super.viewDidLoad() 

    // Do any additional setup after loading the view. 
    tableView.register(UINib(nibName: TableViewCell1.NibName, bundle: nil), forCellReuseIdentifier: TableViewCell1.Identifier) 
    tableView.register(UINib(nibName: TableViewCell2.NibName, bundle: nil), forCellReuseIdentifier: TableViewCell2.Identifier) 
    tableView.register(UINib(nibName: TableViewCell3.NibName, bundle: nil), forCellReuseIdentifier: TableViewCell3.Identifier) 
} 

にあなたは、それぞれ、そのサブビューで、3つのカスタムテーブルビューのセルを持っています:

class MyCellTableViewCell1: UITableViewCell { 
    static var NibName: String = String(describing: TableViewCell1.self) 
    static var Identifier: String = "TableViewCell1" 

    func configure() { ... } 
} 

次に:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell: UITableViewCell 
    // link my custom Cell here. 

    if indexPath.row == 0, let dequeuedCell = tableView.dequeueReusableCell(withIdentifier: TableViewCell1.Identifier, for: indexPath) as? TableViewCell1 { 
     dequeuedCell.configure() 
     cell = dequeuedCell 
    } 
    else if indexPath.row == 1, let dequeuedCell = tableView.dequeueReusableCell(withIdentifier: TableViewCell2.Identifier, for: indexPath) as? TableViewCell2 { 
     dequeuedCell.configure() 
     cell = dequeuedCell 
    } 
    else if indexPath.row == 2, let dequeuedCell = tableView.dequeueReusableCell(withIdentifier: TableViewCell3.Identifier, for: indexPath) as? TableViewCell3 { 
     dequeuedCell.configure() 
     cell = dequeuedCell 
    } 
    else { 
     cell = UITableViewCell() 
    } 


    return cell 

} 
+0

あなたの答えをありがとう!ここでのコメントすべてから、私はストーリーボードで3つのセルを作り、それぞれリユース識別子を与える必要があるようです!カスタムセルを作成します。正直言って、私はストーリーボードに複数の細胞を作ることができないことを知りました(私はプロトタイプ細胞1個でいくつかの細胞を作っていました。私はそれを試してみます!もう一度ありがとう! – George

1

登録する複数のnibファイルが、それは、のviewDidLoad

tableView.register(UINib(nibName: "TableViewCell1", bundle: nil), forCellReuseIdentifier: CellIdentifier1) 

    tableView.register(UINib(nibName: "TableViewCell2", bundle: nil), forCellReuseIdentifier: CellIdentifier2) 

    tableView.register(UINib(nibName: "TableViewCell3", bundle: nil), forCellReuseIdentifier: CellIdentifier3) 
+0

私のcellClassを私のtableViewControllerクラス(viewDidLoad)に登録しました。そしてtextViewは本当に登場しました!!以前はイメージピッカーであった最初のセルに現れました。私は、カスタムセルファイルでtextViewを定義したが、私のtableViewControllerでイメージピッカー(最初の行セル)を定義していたためだと思いますか?私は再使用識別子を持つプロトタイプのセルを1つしか使用していないので、それもそれを引き起こしている可能性があります。 – George

関連する問題