2016-12-12 5 views
0

興味深い問題があります。私はスウィフトの新しい人です。カスタムセル内にカスタムセルを追加する

私はTableViewで作成し、CUSTOM CELLをStoryboardを使用して追加しました。今すぐ別のものを追加したいカスタムセル最初にクリックするときカスタムセル UIButton。

2番目のカスタムセルは、XIBを使用して作成されます。今すぐ登録します。に2番目のセルが登録されました。です.2番目のカスタムセルが空白であるため、空白のテーブルビューが表示されます。

は、私は次のコードを使用している

ここ
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ 

     let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! Cell 

     cell.nameLbl.text = "Hello hello Hello" 

     let Customcell = tableView.dequeueReusableCellWithIdentifier("customCell", forIndexPath: indexPath) as! customCell 


     if self.Selected == "YES" { 
      if self.selectedValue == indexPath.row { 


       return Customcell 
      } 

      return cell 

     } 
     else{ 

      return cell 
     } 
    } 

Cellオブジェクトのインデックスで行の第二の細胞

self.tableView.registerNib(UINib(nibName: "customCell", bundle: nil), forCellReuseIdentifier: "customCell") 

とセルを登録するための

をストーリーボードの細胞のためのものであり、CustomcellはXIBのためであります2番目のカスタムセル。

どうすればいいのか教えてください。

override func viewDidLoad() { 
    super.viewDidLoad() 
    tableView.register(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "customCell") 
} 

答えて

1

まず、あなたのViewControllerをのtableViewのUITableViewDelegateとUITableViewDataSourceあり、そしてあなたはviewDidLoadメソッドでカスタムセルを登録する必要がありますがのtableView

次のための出口を持っていることを確認してください押されたときに1つ以上のセルを変更する場合は、選択したセルの配列を保存するのが最も簡単です。セルが選択されている場合(それが既にカスタムセルでない場合)

var customCellIndexPaths: [IndexPath] = [] 

あなたは、単にカスタムセルIndexPathsの配列に追加することができ、その後、そのセルをリロード:これはViewControllerを内の変数を指定できます。 cellForRowAt法で

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    if customCellIndexPaths.contains(indexPath) == false { 
     customCellIndexPaths.append(indexPath) 
     tableView.reloadRows(at: [indexPath], with: .automatic) 
    } 
} 

我々は、セルが選択されているかどうかをチェックし、その場合は、通常のセルを返す他に、カスタムセルを返す必要があります。

あり
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    if customCellIndexPaths.contains(indexPath) { 
     return tableView.dequeueReusableCell(withIdentifier: "customCell")! 
    } 

    let cell = UITableViewCell(style: .default, reuseIdentifier: "normalCell") 
    cell.textLabel?.text = "Regular Cell" 
    return cell 
} 

あなたはそれを持っています。今度は、選択されたときに、通常のセルがCustomCellになる滑らかなアニメーションを受け取る必要があります。

関連する問題