2017-03-15 3 views
0

ライブラリを使用せずにカスタムセルでインデックスパスに行のサブ行を追加します。didselectRowAt(Swift 3)のときにサブローを追加するにはどうすればいいですか?

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

    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {    
     return arrayEnum.count 
    } 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     var CellTB: UITableViewCell! 

      let cell = tableView.dequeueReusableCell(withIdentifier: "enumCell", for: indexPath)as! UserFlagTableViewCell 
      cell.UserflagLabel.text = arrayEnum[indexPath.row].rawValue 
      CellTB = cell 


//   let cell1 = tableView.dequeueReusableCell(withIdentifier: "freeTextCell", for: indexPath)as! FreeTextTableViewCell 
//   cell1.sendButton.addTarget(self, action: Selector("sendUserflagEvent:"), for: .touchUpInside) 
//   CellTB = cell1 


       return CellTB 

    } 
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 


     self.tableView.insertRows(at: [IndexPath(row: UserFlagType.userFlagTypes.count-1, section: 0)], with: .automatic) 



     self.tableView.beginUpdates() 
     self.tableView.endUpdates() 


    } 
:それは私がこれを試すと同じ

で表示されますそのセル:私は、セルを選択すると

は、別のセルは、この操作は、各セル Psのために繰り返され、このセルの下に表示されます

私は6行

答えて

0

の代わりにあなたのarray.countに

を1を追加することがあります1節 を持っています10

1つ以上のオブジェクトをこの配列に追加します。そうしないと、データソース配列よりも多くの行を取得しようとしているというエラーが表示されるためです。

ので:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
//Add a row here to your array 
self.tableView.insertRows(at: [IndexPath(row: array.count - 1, section: 0)], with: .automatic) 
self.tableView.beginUpdates() 
self.tableView.endUpdates() 
} 

は動作するはずです。 [IndexPath(行:CurrentIndex、セクション:0)]、とあなただけで(arrayEnumに新しい要素を挿入し、その後

tableView.beginUpdates() tableView.insertRows正しいをやっている

+0

に反映されません新しいセル?? 理由: '行6をセクション0に挿入しようとしましたが、更新後にセクション0に6行しかありません' libC++ abi.dylib:NSExceptionタイプのキャッチされない例外で終了します。 –

+0

IndexPathは0から始まり、 array.count - 1を実行する必要があります。私は答えを編集します。 – Mellao

+0

私は列挙型のリストを持っています(6列挙型)私はテーブルビューでそれを作成します。私は、新しいカスタムセルが表示されるセルを選択するとき(ボタンとテキストをどのように送信するか)を指定します。私はどうしているのか分からない。と私はできない列挙型の列挙を追加する –

0

こんにちは: .automatic) tableView.endUpdates()

はここCurrentIndexあなたは配列の要素を挿入したい要素のインデックスでは、それが自動的にそれは私のために働いていないとどのように表示することができます何のテーブルビュー

関連する問題