如何在Swift中将新单元格插入UITableView [英] How to insert new cell into UITableView in Swift
问题描述
我正在开发一个项目,当我有两个 UITableView
和两个 UITextField
s时,用户按下按钮,第一个 textField
中的数据应进入 tableView
,第二个进入第二个的tableView
。我的问题是我不知道如何在每次用户按下按钮时将数据放入 tableView
,我知道如何使用插入数据tableView:cellForRowAtIndexPath:
但据我所知,这有效一次。那么每次用户点击按钮时,我可以用什么方法来更新 tableView
?
I'm working on a project where I have two UITableView
s and two UITextField
s, when the user presses the button the data in the first textField
should go into the tableView
and the second go into the second tableView
. My problem is that I don't know how to put data in the tableView
each time the user pushes the button, I know how to insert data with tableView:cellForRowAtIndexPath:
but that works for one time as far as I know. So what method can I use to update the tableView
each time the user hits the button?
推荐答案
使用 beginUpdates
和 endUpdates
在单击按钮时插入新单元格。
Use beginUpdates
and endUpdates
for insert new cell when button clicked..
首先在tableview数组中附加数据
First of all append data in your tableview array
Yourarray.append([labeltext])
然后升级你的桌子并插入新行
Then upate your table and insert new row
// Update Table Data
tblname.beginUpdates()
tblname.insertRowsAtIndexPaths([
NSIndexPath(forRow: Yourarray.count-1, inSection: 0)
], withRowAnimation: .Automatic)
tblname.endUpdates()
这会插入单元格而不需要重新加载整个表但是如果你遇到任何问题,你也可以使用 tableview.reloadData()
This inserts cell and doesn't need to reload whole table but If you get any problem with this,you can also use 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];
这篇关于如何在Swift中将新单元格插入UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!