【问题标题】:UITableViewCell changing heightUITableViewCell 改变高度
【发布时间】:2016-03-15 19:41:19
【问题描述】:

我想改变表格视图中被触摸行的高度(所以它会扩展):

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
}

这是有效的 - 当我点击该行时高度变为 80.0,当我再次点击它时又回到 50.0。问题是我的分离器没有移动。

它不仅不动,而且当我点击单元格时:

  1. 高度扩大 ->
  2. 分接的单元格分隔符保持在同一个位置 ->
  3. 上面单元格的分隔符消失了

我做错了什么或者我在这里遗漏了什么?

编辑

我试图覆盖 layoutSubviews 方法并忘记了 - 我没有在其中调用 super.layoutSubviews()。现在所选单元格的分隔符随之向下移动,但我仍然遇到上方单元格的分隔符消失的问题

【问题讨论】:

    标签: ios swift uitableview


    【解决方案1】:

    尝试重新加载该单元格

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

    【讨论】:

    • 在 CustomTableViewCell 类中的方法 awakeFromNib 尝试 self.clipsToBounds = true
    • 我太笨了。我试图覆盖布局子视图,但没有在该方法中调用 super。现在所选单元格的分隔符正在向下移动,但我仍然遇到上述单元格的分隔符消失的问题
    • 试过了,对分隔符不下移没有影响,对现有问题也没有
    • 你可以分享截图吗?这将更有利于理解问题。
    【解决方案2】:

    如果您不必选择您的单元格,则此线程应该是答案:

    UITableView separator line disappears when selecting cells in iOS7

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-24
      • 1970-01-01
      • 2014-03-27
      • 2013-06-15
      • 2014-11-23
      相关资源
      最近更新 更多