ios - 更改节中特定索引的行高

标签 ios swift uitableview

我一直在设置我的表格 View 的单元格高度

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    switch indexPath.section {
    case 0:
        return 100
    case 1:
        tableView.estimatedRowHeight = 100
        return  UITableViewAutomaticDimension
    default:
        return 0
    }
}

这样,对于第 1 部分中的所有行,行高设置为 100。现在,我想在单击按钮时将特定行的行高更改为 0(即使其隐藏)。

我已经尝试过:

tableView.cellForRow(at: IndexPath(item: selectedIndex, section: 1))?.isHidden

虽然单元格被隐藏,但我留下了一个占据行高的空白单元格。

所以我认为我应该将行高更改为 0。

附注我知道这可以通过从模型中删除特定行并使用deleteRows(at indexPaths: [IndexPath], 带动画:UITableView.RowAnimation)但我不想更改模型。如何做到这一点?

最佳答案

使用标志来检查您是否单击了该按钮。然后单击按钮后重新加载表格。

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return indexPath.section == 0 ? buttonClicked && indexPath.row == 0 ? CGFloat.leastNormalMagnitude : 100 : UITableViewAutomaticDimension
}

更长、更易读的版本:-

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

switch indexPath.section {
case 0:
    if buttonClicked && indexPath.row = rowIndex //index of required row which you can find out and store in a variable wherever necessary {
        return CGFloat.leastNormalMagnitude
    }
    return 100
case 1:
    tableView.estimatedRowHeight = 100
    return  UITableViewAutomaticDimension
default:
    return 0
}
}

关于ios - 更改节中特定索引的行高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51298614/

相关文章:

ios - 如何让我的 iOS 应用程序连续循环播放声音列表?

IOS swift 如何使用 UIActivityViewController 共享图像和文本

ios - 在 Swift 中使用 commitEditingStyle 动态删除 UITable 部分

ios - 适用于 iOS 的 Color Vignette Core Image 过滤器?

ios - PrepareForSegue 始终返回索引 0 处的单元格

ios - 使用 NSTemporaryDirectory 写入视频文件

ios - 如何使用 Swift 在 iOS 应用程序中接收/导入共享数据?

iphone - 自动调整 UITableViewCell 内容宽度

iphone - UILabel 异常显示的极其奇怪的事情

ios - x264 编码使用 x264_picture_clean 崩溃