2017-03-15 50 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() 


    } 
:其中它会出现这是相同的

我尝试这种细胞

我有1节 我有6行

回答

0

而不是增加1到你的array.count的

self.tableView.insertRows(at: [IndexPath(row: array.count+1, section: 0)], with: .automatic) 

添加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() 
} 

应该工作。

+0

不,这不是为我工作,我怎么能显示新的细胞? 原因:“试图插入一行6成部分0,但更新后只有6个在第0行” 的libC++ abi.dylib:与类型的未捕获的异常NSException –

+0

IndexPath从0开始,而你是终止做array.count,从1开始。所以...你必须做array.count - 1.我将编辑答案,Sry – Mellao

+0

我有一个枚举的列表(6枚)我把它放在表视图和我想要当我选择一个单元格出现一个新的自定义单元格(我有一个按钮和文本发送)。我不知道它是如何做到的。和我不能添加到可能枚举数组 –

0

嗨,你在做正确的只需要插入新的元素在arrayEnum然后

tableView.beginUpdates() tableView.insertRows(在:IndexPath(行:CURRENTINDEX,部分:0)],其中: 。自动) tableView.endUpdates()

在要插入在数组中的元素的元素的索引这里CURRENTINDEX,它会自动在tableview中反映