【问题标题】:How to change tableview section cell height when click on specific cell?单击特定单元格时如何更改表格视图部分单元格的高度?
【发布时间】:2021-10-28 00:17:12
【问题描述】:

我在表格视图单元格中有很多部分,每个部分包含许多单元格。我需要在单击单元格时放大单元格。现在当我单击单元格时,该部分内的所有单元格高度都会发生变化。请帮我解决这个问题

     var expandedIndexSet : IndexSet = []

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if expandedIndexSet.contains(indexPath.section) {
        return 406
    } else {
        return 88
    }
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
 
    if(expandedIndexSet.contains(indexPath.section)){
        expandedIndexSet.remove(indexPath.section)
    } else {
        expandedIndexSet.insert(indexPath.section)
    }
    tableView.reloadRows(at: [indexPath], with: .automatic)
}  

【问题讨论】:

    标签: ios swift uitableview tableview


    【解决方案1】:

    您只存储部分索引。取而代之的是,您需要存储 IndexPath 对象:

    var expandedIndexSet = Set<IndexPath>()
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if expandedIndexSet.contains(indexPath) {
            return 406
        } else {
            return 88
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
     
        if(expandedIndexSet.contains(indexPath)){
            expandedIndexSet.remove(indexPath)
        } else {
            expandedIndexSet.insert(indexPath)
        }
        tableView.reloadRows(at: [indexPath], with: .automatic)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-04
      相关资源
      最近更新 更多