首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TableView -未调用heightForRowAtIndexPath

TableView -未调用heightForRowAtIndexPath
EN

Stack Overflow用户
提问于 2017-02-02 23:53:18
回答 4查看 7.9K关注 0票数 5

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

代码语言:javascript
复制
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从未被调用过。视图控制器是一个UITableViewController,其他一切都能正常工作。你们中有没有人看到这个函数没有被调用的原因?

谢谢你的帮助!

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-02-02 23:55:33

在Swift 3中,签名是:

代码语言:javascript
复制
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat

您使用的是旧签名,因此无法识别它。

代码语言:javascript
复制
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if indexPath.row == 1 {
        return UITableViewAutomaticDimension * 3

    }
    return UITableViewAutomaticDimension
}
票数 17
EN

Stack Overflow用户

发布于 2018-05-30 12:56:26

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

代码语言:javascript
复制
self.tableviewObj.delegate = self

所以,不要忘记将委托添加到self中。因为heightForRowAtIndexparth是在委托协议中声明的。

票数 12
EN

Stack Overflow用户

发布于 2019-04-30 15:58:39

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42006650

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档