ios - 多次调用tableView.reloadData会不会导致性能问题?

标签 ios swift uitableview

我只是想非常优雅地在 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()
    }
}

现在我只需调用 removeCellFromSectionaddCellToSection 即可添加和删除单元格。多么优雅!

但是,我不确定是否一直调用 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/

相关文章:

ios - Swift 中 SKProduct() 的应用内购买问题

ios - 返回上一个屏幕时如何在 Controller 之间传输数据? swift

ios - 为什么我的矩形画错了?

ios - 如何以分组样式设置 UITableView 中单元格的宽度

ios - 自定义 UITableview 单元格偶尔会在其他单元格之上重叠/复制

ios - 将不同的数据和不同长度的项目分配给 UITableView 内的 UICollectionView

ios - 如何加载更多到 collectionview ?

iphone - 键盘弹起时 Facebook 对话框不旋转

ios - 单元测试覆盖率%0

ios - 如何用动画改变 UITableView 的宽度?