Expand only the cell that has been tapped

*爱你&永不变心* 提交于 2021-02-07 04:28:20

问题


I have some code in my app that expands a cell when it is tapped. The problem is that I haven't figured out how to only expand the cell that has been tapped. At the moment my code just expands all of the cells.

Here is my code:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    selectedRowIndex = indexPath
    tableView.beginUpdates()
    tableView.endUpdates()
}

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if indexPath.row == selectedRowIndex.row {
        if cellTapped == false {
            cellTapped = true
            return 141
        } else {
            cellTapped = false
            return 70
        }
    }
    return 70
}

What do I need to do to only expand the cell that has been tapped?


回答1:


The following worked for me. The cells are 70 high and expand when tapped :

class ViewController: UITableViewController {
    var cellTapped:Bool = true
    var currentRow = 0;

Then I slightly modified the following two methods:

 override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var selectedRowIndex = indexPath
    currentRow = selectedRowIndex.row

    tableView.beginUpdates()
    tableView.endUpdates()
}

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if indexPath.row == currentRow {
        if cellTapped == false {
            cellTapped = true
            return 141
        } else {
            cellTapped = false
            return 70
        }
    }
    return 70
}


来源:https://stackoverflow.com/questions/26318209/expand-only-the-cell-that-has-been-tapped

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!