【问题标题】:Change only one specific tableView row height [duplicate]仅更改一个特定的 tableView 行高 [重复]
【发布时间】:2020-05-26 07:34:04
【问题描述】:

我正在寻找一种方法来更改我的 tableView 中的特定行。

我正在使用通知来检查我何时在单元格中执行操作。根据答案,我的目标是显示下一行。

默认情况下,我的单元格有这个属性。

if (indexPath.row == 5){
    tableView.rowHeight = 0.0
}

if (indexPath.row == 6){
    tableView.rowHeight = 0.0
}

return cell

我在通知中的目标是更改第五行的行高值。

感谢您的帮助

【问题讨论】:

  • 谢谢,但不,因为我想直接从通知中更改行的高度,而不是在 tableView 函数中
  • 您需要在点击操作时更改单元格行吗?
  • 是的,这是我的目标。在我的应用中,我正在使用单选项目,所以如果我选择一个特定项目,我想显示下一个

标签: swift uitableview tableview row-height


【解决方案1】:

您可以使用 Set<IndexPath> 和您的 tableView 委托方法来实现此目的。

假设您有一组选定的索引路径selectedIndexPaths 和高度largeHeightnormalHeight。您的 heightForRow 函数可能如下所示:

func tableView(_ tableView: UITableView, heigthForRowAt indexPath: IndexPath) -> CGFloat {
    guard !selectedIndexPaths.contains(indexPath) else {
        return largeHeight
    }

    return normalHeight
}

然后您可以通过以下方式动态更改高度:

/// Convenience method for selecting an index path
func select(indexPath: IndexPath, completion: ((Bool) -> Void)? = nil){
    selectedIndexPaths.insert(indexPath)
    tableView.performBatchUpdates({
        self.tableView.reloadRows(at: [indexPath], with: .none)
    }, completion: completion)
}

在您的 tableView 委托中,您可以在 didSelect 中调用此方法:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    select(indexPath: indexPath)
}

如果您有响应通知的方法(假设您将 indexPath 放在通知的 userInfo 中,在键“indexPathKey”下),请执行相同操作:

func notifiedShouldEnlargeRow(aNotification: Notification) {
    guard let indexPath = aNotification.userInfo["indexPathKey"] as? IndexPath else { return }
    select(indexPath: indexPath)
}

作为参考,请查看performBatchUpdates(_:completion)reloadRows(at:with:)

【讨论】:

  • 并非所有代码都是必需的,但我通过self.tableView.reloadRows(at: [indexPath], with: .none) 找到了解决方案,谢谢
  • 只是想我会放弃一个完整的工作示例。 :) 很高兴它有帮助!
猜你喜欢
  • 2017-07-01
  • 2015-04-21
  • 1970-01-01
  • 2011-12-07
  • 2020-09-24
  • 2016-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多