我正在开发一个项目,其中有两个 UITableView
和两个 UITextField
,当用户按下按钮时,第一个 textField 中的数据
应该进入 tableView
,第二个进入第二个 tableView
。我的问题是,我不知道如何在每次用户按下按钮时将数据放入 tableView
中,我知道如何使用 tableView:cellForRowAtIndexPath:
插入数据,但是据我所知,这一次有效。那么每次用户点击按钮时我可以使用什么方法来更新 tableView
呢?
最佳答案
单击按钮时,使用 beginUpdates
和 endUpdates
插入新单元格。
As @vadian said in comment,
begin/endUpdates
has no effect for a single insert/delete/move operation
首先,将数据附加到表格 View 数组中
Yourarray.append([labeltext])
然后更新表格并插入新行
// Update Table Data
tblname.beginUpdates()
tblname.insertRowsAtIndexPaths([
NSIndexPath(forRow: Yourarray.count-1, inSection: 0)], withRowAnimation: .Automatic)
tblname.endUpdates()
这会插入单元格,不需要重新加载整个表格,但如果您遇到任何问题,您也可以使用tableview.reloadData()
swift 3.0
tableView.beginUpdates()
tableView.insertRows(at: [IndexPath(row: yourArray.count-1, section: 0)], with: .automatic)
tableView.endUpdates()
Objective-C
[self.tblname beginUpdates];
NSArray *arr = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:Yourarray.count-1 inSection:0]];
[self.tblname insertRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tblname endUpdates];
关于ios - 如何在 Swift 中将新单元格插入到 UITableView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57235361/