我正在做一个项目,我有两个UITableView
和两个UITextField
,当用户按下按钮时,第一个textField
中的数据应该进入tableView
,第二个进入第二个tableView
.我的问题是,我不知道如何在用户每次按下按钮时将数据放入tableView
,我知道如何插入tableView:cellForRowAtIndexPath:
的数据,但据我所知,这只适用于一次.那么,每次用户点击按钮时,我可以使用什么方法来更新tableView
?
我正在做一个项目,我有两个UITableView
和两个UITextField
,当用户按下按钮时,第一个textField
中的数据应该进入tableView
,第二个进入第二个tableView
.我的问题是,我不知道如何在用户每次按下按钮时将数据放入tableView
,我知道如何插入tableView:cellForRowAtIndexPath:
的数据,但据我所知,这只适用于一次.那么,每次用户点击按钮时,我可以使用什么方法来更新tableView
?
单击按钮时,使用beginUpdates
和endUpdates
插入新单元格.
正如@vadian在 comments 中所说,
begin/endUpdates
对单个插入/删除/移动操作没有影响
首先,在表视图数组中追加数据
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];