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

Time:09-17

i have many sections in table view cell each section contain many cell. i need to enlarge the cell while click on the cell .now when i click on cell all cells height changing inside the section.please help me to solve this

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

CodePudding user response:

You're storing only section indices. Instead of that you need to store IndexPath objects:

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