我只是想非常优雅地在 TableView 中添加和删除行。所以我问了这个question .但我认为答案没有回答我的问题。
然后我想我可以创建一个 UITableViewCell
数组并将其用作数据源!
所以我做了这样的事情:
class MyTableViewController: UITableViewController {
var cells = [[UITableViewCell]]()
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return cells.count
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cells[section].count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return cells[indexPath.section][indexPath.row]
}
func addCellToSection(section: Int, cell: UITableViewCell) {
cells[section].append(cell)
tableView.reloadData()
}
func removeCellFromSection(section: Int, index: Int) {
cells[section].removeAtIndex(index)
tableView.reloadData()
}
}
现在我只需调用 removeCellFromSection
和 addCellToSection
即可添加和删除单元格。多么优雅!
但是,我不确定是否一直调用 tableView.reloadData()
会降低应用程序的速度。每次我添加或删除一个单元格时,表格 View 中的每个单元格都需要刷新。工作量会很大,对吧?
那么一直调用 tableView.reloadData()
会显着降低我的应用程序速度吗?如果是,可以选择什么?如果不是,为什么不呢?
附加信息:
最初, TableView 包含 4 个单元格。用户可以通过按下按钮来添加和删除单元格。平均而言,用户会添加少于 50 个单元格。
最佳答案
如果你有你的单元格的indexPath
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPathOfYourCell, nil] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
它可能对你有帮助。
关于ios - 多次调用tableView.reloadData会不会导致性能问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37290861/