【问题标题】:TableView - heightForRowAtIndexPath not being calledTableView - heightForRowAtIndexPath 没有被调用
【发布时间】:2017-02-02 15:53:18
【问题描述】:

在浏览完所有其他 Stack Overflow 表单后,我为我的一个单元格实现了一个动态高度,如下所示:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell

    if(indexPath.row == 1){
        var image : UIImage = maskRoundedImage(image: UIImage(named: "Temp_Profile.png")!, radius: 150)
        cell.imageView?.image = image
        return cell
    }
    cell.textLabel?.text = TableArray[indexPath.row]
    cell.backgroundColor = UIColor.init(colorLiteralRed: 0.56, green: 0, blue: 0.035, alpha: 1)
    cell.textLabel?.textColor = UIColor.white
    cell.textLabel?.font = UIFont(name: "Helvetica", size: 28)

    return cell
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if  indexPath.row == 1 {
        return UITableViewAutomaticDimension * 3

    }
    return UITableViewAutomaticDimension
}

看起来很简单,但调试会话显示 heightForRowAtIndexPath 从未被调用。 View Controller 是 UITableViewController ,其他一切正常。你们有没有看到这个函数没有被调用的任何原因?

感谢您的帮助!

【问题讨论】:

  • 如下所述,UITableViewAutomaticDimension 不是应该用于算术的值。返回 UITableViewAutomaticDimension * 3 会产生不可预知的效果。
  • 请注意,调用heightForRowAt 可能很昂贵;您也可以在viewDidLoad 中致电self.tableView.rowHeight = UITableViewAutomaticDimensionself.tableView.estimatedRowHeight = 72.0 //estimated, average value。也可以在这里查看:appcoda.com/self-sizing-cells

标签: ios swift uitableview heightforrowatindexpath


【解决方案1】:

在 Swift 3 中,签名是:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat

您有旧签名,因此无法识别。

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if indexPath.row == 1 {
        return UITableViewAutomaticDimension * 3

    }
    return UITableViewAutomaticDimension
}

【讨论】:

  • 对于任何尝试使用 UITableViewAutomaticDimension * 3 的人,请不要。它返回一个 -1 作为一个信号给 tableView 以使其成为默认值。使用实际数字
  • 当您从其他地方复制示例时,很容易得到这些方法的旧签名 - 我们都这样做!。在某些情况下,如果 Swift 已经足够让事情变得无效,XCode 会产生一个错误——但对于这样的情况,你只会得到一个警告。那就是你不应该忽略那些黄色三角形。自动完成应该给你正确的签名,然后函数的其余部分很可能就可以了。
【解决方案2】:

对我来说,我必须这样做。

self.tableviewObj.delegate = self

所以,不要忘记给自己添加委托。因为在委托协议中声明了 heightForRowAtIndexparth。

【讨论】:

  • 是的,在 viewDidLoad 方法上添加上述代码后,它就起作用了。谢谢
【解决方案3】:

在我的情况下,我有三个控制器A,B,C。A是基本控制器,B继承自A,C继承自B。我在A中设置tableView委托,并在B中实现heightForRowAt函数,问题当我使用C时发生。我猜是因为A中分配的代理最终指向C,所以B中的方法没有被调用。

【讨论】:

  • 如果您有新问题,请点击 按钮提出问题。如果您有足够的声誉,you may upvote 的问题。或者,将其“加星标”作为收藏夹,您将收到任何新答案的通知。
  • 在这种情况下,(我也是),我发现解决方案是在基类中定义一个占位符方法调用,然后在后代中覆盖它。然后它被调用。
【解决方案4】:

如果您在UITableViewController 中,请记住该方法必须是您需要override 的方法,否则方法名称错误。

在我的情况下,我使用的是 NSIndexPath 而不是 IndexPath,它与要调用的正确方法不匹配

tableView(_ tableView: UITableView, heightForRowAt indexPath: NSIndexPath) -> CGFloat

当我更正它时

tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat

xcode 会提示插入 override 关键字。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多