[英]how can i add subrow when didselectRowAt ( Swift 3)
I want to add a sub row in row at index path with custom cell without using a library. 我想在不使用库的情况下使用自定义单元格在索引路径的行中添加子行。
When I select a cell, another cell appears below this cell, this operation is repeated for each cell Ps: the cell of which it will appear it is the same 当我选择一个单元格时,另一个单元格出现在该单元格的下方,对每个单元格Ps重复此操作:将要出现的单元格是相同的
I try this: 我尝试这样:
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()
}
i have 1 section i have 6 rows 我有1节,我有6行
Instead of adding 1 to your array.count 而不是添加1到您的array.count
self.tableView.insertRows(at: [IndexPath(row: array.count+1, section: 0)], with: .automatic)
Add 1 more object to this array, because if not, you will get an error saying that you are trying to get more rows than your datasource array. 向该数组再添加1个对象,因为如果没有,则将收到一条错误消息,提示您试图获取比数据源数组更多的行。
So: 所以:
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()
}
should work. 应该管用。
Hi you are doing correct just insert the new element in the arrayEnum and then 嗨,您做得正确,只需在arrayEnum中插入新元素,然后
tableView.beginUpdates() tableView.insertRows(at: [IndexPath(row:CurrentIndex , section: 0)], with: .automatic) tableView.endUpdates() tableView.beginUpdates()tableView.insertRows(at:[IndexPath(row:CurrentIndex,section:0)],带有:.automatic)tableView.endUpdates()
Here CurrentIndex in an index of element where you want to insert an element in array , it will automatically reflect in tableview 在这里,CurrentIndex在要在数组中插入元素的元素索引中,它将自动反映在tableview中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.