ios - 展开/折叠最后一个单元格后奇怪的 UITableView 行为

标签 ios swift uitableview

我想要实现的是:当单击单元格内部的标签时,单元格可以展开/折叠。应记住单元格大小,以便当用户滚动回单元格时,它应该是原来的大小。我使用自动布局设置单元格,并为所有 View 分配了高度约束。当用户单击标签时,我通过更改标签的高度约束来更改单元格高度:

func triggerExpandCollapse(_ cell: UITableViewCell) {
    guard let cell  = cell as? ActivityTableViewCell, let indexPath = self.activitiesTableView.indexPath(for: cell) else { return }

    if !self.expandedCellIndex.insert(indexPath.row).inserted {
        self.expandedCellIndex.remove(indexPath.row)
        cell.descLabelHeightConstraint.constant = 60
    }
    else {
        cell.descLabelHeightConstraint.constant = cell.descLabel.intrinsicContentSize.height
    }

    self.activitiesTableView.beginUpdates()
    self.activitiesTableView.endUpdates()
}

并在返回每个单元格之前配置单元格的高度:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: Constants.TableViewCellID.activity, for: indexPath)
    guard let activityCell = cell as? ActivityTableViewCell else {
        return cell
    }

    activityCell.delegate = self
    if self.expandedCellIndex.contains(indexPath.row) {
        activityCell.descLabelHeightConstraint.constant = activityCell.descLabel.intrinsicContentSize.height
    }
    else {
        activityCell.descLabelHeightConstraint.constant = 60
    }
    activityCell.layoutIfNeeded()
    return activityCell
}

我还通过以下方式启用自动布局:

self.activitiesTableView.rowHeight = UITableViewAutomaticDimension
self.activitiesTableView.estimatedRowHeight = 1000

到目前为止,除了最后一个单元格之外,单元格可以通过正常的滚动行为展开/折叠!当我展开最后一个并向上滚动时,activitiesTableView 看起来“跳跃”了一小段距离。详细可以引用video

我现在不知道如何调试。有人对此有一些想法吗?谢谢。

最佳答案

在睡了一觉来理清思绪后,我进行了一些尝试,并找到了一种方法来展开/折叠单元格并带有动画和平滑的 UITableView 滚动,我所做的可以用以下更改来描述:

  1. 在动画 block 中包含单元格的 layoutIfNeededbeginUpdatesendUpdates 来触发单元格展开/折叠。完整版是:

    func triggerExpandCollapse(_ cell: UITableViewCell) {
        guard let cell = cell as? DonationsTableViewCell, let indexPath = self.donationsTableView.indexPath(for: cell) else { return }
    
        if !self.expandedCellsSet.insert(indexPath.row).inserted    // Collapse
        {
            self.expandedCellsSet.remove(indexPath.row)
            cell.projectDescLabelHeightConstraint.constant = 60
        }
        else    // Expand
        {
            cell.projectDescLabelHeightConstraint.constant = cell.projectDescLabel.intrinsicContentSize.height
        }
    
        UIView.animate(withDuration: Constants.TimeDuration.cellExpandCollapse, delay: 0, options: .curveEaseInOut, animations: { 
            cell.layoutIfNeeded()
            self.donationsTableView.beginUpdates()
            self.donationsTableView.endUpdates()
        }, completion: nil)
    }
    
  2. 在这种情况下,滚动指示器会在用户滚动时更改其大小。这是因为 estimatedRowHeight 与实际单元格高度相差太大。我建议实现 tableView(_:estimatedHeightForRowAt:) 来提供每个单元格的大致高度。注意,如果返回值太大,展开/折叠行为会异常

如果有人有更好的解决方案,我将非常感激!

关于ios - 展开/折叠最后一个单元格后奇怪的 UITableView 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42469463/

相关文章:

ios - ibtool 失败,退出代码为 1 xcode 8.2.1

ios - Xcode 6.3 警告

如果我 readValue,iOS BLE 外设 writeValue 不起作用

ios - swift indexPath.row==0 对 UITableViewCell 的工作很奇怪

ios - 更改 tableView 中所有单元格的所有对象的 bool 状态

ios - 模拟器/设备不与 iCloud 同步

ios - 是否可以从其父 View 的边界中取消剪裁单个 subview ?

generics - 如何将 Swift 协议(protocol)限制为具体类型?

swift - 在 Swift 4 中向数组添加过滤器

ios - 将 subview 添加到 self.tableView.tableHeaderView 不起作用