ios - 如何在 Swift 中将新单元格插入到 UITableView 中

标签 ios iphone swift uitableview

我正在开发一个项目,其中有两个 UITableView 和两个 UITextField,当用户按下按钮时,第一个 textField 中的数据 应该进入 tableView ,第二个进入第二个 tableView 。我的问题是,我不知道如何在每次用户按下按钮时将数据放入 tableView 中,我知道如何使用 tableView:cellForRowAtIndexPath: 插入数据,但是据我所知,这一次有效。那么每次用户点击按钮时我可以使用什么方法来更新 tableView 呢?

最佳答案

单击按钮时,使用 beginUpdatesendUpdates 插入新单元格。

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/

相关文章:

ios - Swift - iOS NSPredicate 带有可选的 NSDecimalNumber

iphone - NSDate到NSString的转换提供了不同的输出

iOS collectionView.sizeForItemAtIndexPath 在 iPhone 6 之前中断

ios - CGRect 与 4 个矩形相交。如何找出它与哪个矩形最相交?

IOS - 如何应用恒定速度 - Swift 3

ios - 在iOS键盘上覆盖自定义键盘

android - 如何限制移动应用中的Google Map API key ?

swift - 如何更新已编译框架的版本

iOS : Black screen appears when run my app

ios - 我如何在 CloudKit 中创建一个记录作为 child 的记录列表?