单击特定单元格时如何更改表格视图部分单元格高度?

How to change tableview section cell height when click on specific cell?

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

     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)
}  

您只存储部分索引。相反,您需要存储 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)
}