简体   繁体   English

UITableViewCell更改高度

[英]UITableViewCell changing height

I want to change height of the touched row in table view (so it expands): 我想在表格视图中更改触摸行的高度(因此它会扩展):

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(indexPath) as! CustomTableViewCell
    if let currentSelection = self.currentSelection {
        if currentSelection == indexPath {
            tableView.deselectRowAtIndexPath(indexPath, animated: true)
            self.currentSelection = nil
            tableView.beginUpdates()
            tableView.endUpdates()
            setupAnimation(cell, hide: true) //Custom animation inside the cell
        }
    } else {
        self.currentSelection = indexPath
        tableView.beginUpdates()
        tableView.endUpdates()
        setupAnimation(cell, hide: false) //Custom animation inside the cell
    }
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if let currentSelection = self.currentSelection {
        if indexPath == currentSelection {
            return 80.0
        }
    }
    return 50.0
}

This is working - the height changes to 80.0 when I tap the row, and comes back to 50.0 when I tap it again. 这是可行的-当我点击该行时,高度更改为80.0,而当我再次点击该行时,高度更改为50.0。 The thing is that my separator is not moving. 问题是我的分隔符没有动。

Not only it is not moving, but when I tap the cell: 它不仅没有移动,而且当我点击单元格时:

  1. the height expands -> 高度扩大->
  2. the tapped cell separator stays in the same place -> 抽头式细胞分离器留在同一位置->
  3. the separator of the cell above disappears 上方单元格的分隔符消失了

What am I doing wrong or what am I missing here? 我做错了什么或我在这里想念什么?

EDIT 编辑

I have tried to override layoutSubviews method and forgot about that - I didn't call super.layoutSubviews() in it. 我试图重写layoutSubviews方法并忘记了这一点-我没有在其中调用super.layoutSubviews()。 Now the separator of selected cell is moving down with it but I still have problem with the separator of the cell above disappearing 现在所选单元格的分隔符正在向下移动,但上方单元格的分隔符消失了,我仍然遇到问题

try to reload that cell 尝试重新加载该单元格

tableView.beginUpdates()
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) //try other animations
tableView.endUpdates()

If you don't have to have your cell selected this thread should be the answer: 如果您不必选择单元格,那么此线程应该是答案:

UITableView separator line disappears when selecting cells in iOS7 在iOS7中选择单元格时,UITableView分隔线消失

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM