ios - TableView 在索引路径问题上重新加载行

标签 ios swift uitableview

Apple 文档说:reloadRowsAtIndexPaths:withRowAnimation: :

Call this method if you want to alert the user that the value of a cell is changing. If, however, notifying the user is not important—that is, you just want to change the value that a cell is displaying—you can get the cell for a particular row and set its new value.

我的问题场景是我想在单击按钮时更新单元格中的标签并更新布局(即在应用的条件下添加或删除单元格的新 View )。如果在单击按钮时调用了 reloadRowsAtindexPath,则 tableview 会随机向下滚动到 tableview 中的其他行。如果单击按钮时仅更新单元格内容,则布局未正确更新。

是否有人遇到过与重新加载相同的问题?

最佳答案

结果比我预期的要棘手。

下面是我的实现方式。我不太确定这是否是一个好方法,所以对它持保留态度。在下面找到该项目的链接。

你需要知道两件事:

  1. The default/normal cell height (which is basically the estimated height of cell).
  2. Increase in height of cell after the view has been added to the cell (in my example I have taken the height to be 200).

当您点击一个应该添加 subview 的按钮时,您会调用一个完成处理程序,将 indexPathheightToBeAdded(本例中为 200)作为参数传递。

完成看起来像这样:

self.indexPath = iPath
self.cellHeight = self.defaultCellHeight + heightToBeAdded
UIView.beginAnimations("aaa", context: nil)
UIView.setAnimationDuration(1)
self.tableView.beginUpdates()
self.tableView.reloadRows(at: [iPath], with: .none)
self.tableView.endUpdates()
UIView.commitAnimations()
if heightToBeAdded > 0.0 {
    self.addCellSubviews()
}

iPath 与您发送参数的 indexPath 相同。单元格高度是根据我上面描述的两件事计算出来的。

endUpdates()调用delegate和datasource方法时会遇到如下方法:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    guard let iPath = self.indexPath else { return UITableViewAutomaticDimension }
    if indexPath == iPath {
        return cellHeight
    }
    else {
        return UITableViewAutomaticDimension
    }
}

这将增加单元格高度。但是需要添加的实际 subview 呢?

为此,我们有 addCellSubviews(),它实际上是一个在 UITableViewCell 子类中执行的完成 block 。仅在结束更新后调用此方法,因为届时将正确计算单元格高度。

let yView = UIView()
yView.backgroundColor = .yellow
yView.translatesAutoresizingMaskIntoConstraints = false
self.addAsSubview(sView: yView)
NSLayoutConstraint.activate([
        yView.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 8),
        yView.topAnchor.constraint(equalTo: self.topAnchor, constant: 8),
        yView.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -275),
        yView.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -8)
        ])
UIView.animate(withDuration: 1, animations: {
        self.layoutIfNeeded()
})

这是结果—— enter image description here

Link to the project

请注意有一些注意事项(例如点击一个单元格会关闭另一个单元格),我相信您将能够解决。

关于ios - TableView 在索引路径问题上重新加载行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49726976/

相关文章:

ios - UINavigationController "pushViewController:animated"的完成处理程序?

iphone - 有时不调用 UIWebView 的 shouldStartLoadWithRequest

ios - UIScrollView scrollViewDidEndDragging和scrollViewDidEndDecelerating无法正常工作

java - 我的应用程序禁用了 iOS 推送通知怎么办?

ios - 如何为目标 V​​C 的子项设置协议(protocol)委托(delegate)?

ios - 在重用的 UITableViewCell 中使用动画更改图像

swift - 对 SceneKit 物理体施加力?

swift - 字符串(contentsOfURL :) in Swift 3

ios - 我们如何在 TableView 中的两个给定单元格之间动态添加新单元格?

ios - 如何在 UITableView - Swift 中制作 list ?