ios - 高度从 0 变为默认大小后单元格不显示

标签 ios uitableview swift3

我正在尝试实现一个静态表格 View ,根据用户选择的选项,隐藏或显示不同的表格 View 单元格。

enter image description here

当“Repeat”为“Never”时,不应显示单元格,但当“Repeat”为其他任何内容时,应显示“Repeat End”单元格。

enter image description here

现在的问题是,当我选择从“从不”到任何其他值的重复值时{意味着我已将单元格的状态从隐藏更改为未隐藏} 单元格显示为空白。 enter image description here

我已经使用 heightForRowAt 实现了显示和隐藏

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    let defaultHeight = super.tableView(tableView, heightForRowAt: indexPath)
    let heightIndexPath: [Int: [Int: CGFloat]] = [1:
        [sectionRow.startDateTimePicker.rawValue : startDateEditing ? startDateTimePickerCell.contentView.subviews.first?.frame.height ?? 0 : 0,
         sectionRow.startDateTimeZone.rawValue : startDateEditing ? defaultHeight : 0,
         sectionRow.endDateTimePicker.rawValue : endDateEditing ? endDateTimePickerCell.contentView.subviews.first?.frame.height ?? 0 : 0,
         sectionRow.endDateTimeZone.rawValue : endDateEditing ? defaultHeight : 0,
         sectionRow.repeatEnd.rawValue : repeatValue == Event.Repeat.Never ? 0 : defaultHeight]]
    let cellHeight = heightIndexPath[indexPath.section]?[indexPath.row] ?? defaultHeight
    let cell = self.tableView(tableView, cellForRowAt: indexPath)
    cell.isHidden = cellHeight == 0 ? true : false
    cell.alpha = cellHeight == 0 ? 0 : 1
    return cellHeight
}

我什至尝试更改单元格的 isHidden 和 alpha 值,但它仍然没有显示。但奇怪的是,当我更改值(并非永远不会)时,单元格再次出现。

我是否错过了显示此隐藏单元格的内容?

顺便说一句,当重复值如下更改时,我只刷新隐藏的单元格。我什至尝试重新加载整个表格,但它没有用。

internal var repeatValue = Event.Repeat.Never {
    didSet {
        repeatLabel.text = repeatValue.rawValue
        let indexPaths = [IndexPath(row: sectionRow.repeatEnd.rawValue, section: 1)]
        tableView.reloadRows(at: indexPaths, with: .none)
        // tableView.reloadData() // Tried this also.
    }
}

最佳答案

您应该改写 tableView(tableView:numberOfRowsInSection:)

当你想隐藏行时,调用 super 实现然后减去 1。现在您将能够通过调用为行显示/隐藏设置动画 deleteRows(at:with:)insertRows(at:with:) 视情况而定。

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    var numberOfRows = super.tableView(tableView, numberOfRowsInSection: section)

    if (section == RepeatSection && shouldHideRepeatEnd()) {
        numberOfRows = numberOfRows - 1
    }
    return numberOfRows
}

这样会好看很多

关于ios - 高度从 0 变为默认大小后单元格不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45890438/

相关文章:

ios - 如何在整个应用程序中禁用 NSLog?

ios - UIDatePicker 在刷新之前显示不正确的分钟间隔

ios - 如何使用分割模型输出张量?

ios - 以编程方式使用 swift UITableView

swift - 滑动即可删除单元格,而无需按删除按钮 Swift

swift - 使用 moveRowAt IndexPath 移动 UITableView 中的单元格

ios - 应用程序(_ :didFinishLaunchingWithOptions:)' nearly matches optional requirement

ios - 如何在 Swift 中使用 guard 使用 nil 处理 JSON 响应

ios - 在 Swift 中将 12 小时 UTC 日期转换为 24 小时本地时区格式

Swift:func返回类型继承类并符合一个或多个协议(protocol)