【问题标题】:heightForRowAtIndexPath never being called iOS9heightForRowAtIndexPath 永远不会被称为 iOS9
【发布时间】:2015-10-20 02:18:10
【问题描述】:

好吧,我很确定这个方法一直在为我的UITableViewController 执行直到现在。我的方法中有打印语句,如下所示:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //455(PERFECT MIN HEIGHT) 475 is perfect spacing. 15.5 is the scalar for when descriptionLabel expands lines
    return 475;
    NSLog(@"ISSSS ITTT ONNNN???");

    if (currentDescriptionHeight == 16.0) {
        NSLog(@"OHHH so it triggered 1111");
        return 475;
    }else if (currentDescriptionHeight == 31.5){
        NSLog(@"OHHH so it triggered 2222");
        return 490.5;
    }else if (currentDescriptionHeight == 47){
        NSLog(@"OHHH so it triggered 3333");
        return 506;
    }

    //475 - 1 line description,
}

我已将方法标头复制到我的 .h 文件中,并已正确设置委托:

self.tableView.delegate = self;
self.tableView.dataSource = self;

在我的viewDidLoad: 方法中。为什么我的heightForRowAtIndexPath 根本没有被调用?

【问题讨论】:

  • 为什么函数开头有return语句?
  • 呃,方法错误是因为只在条件句中没有返回?
  • return 语句立即结束函数,因此它下面的代码永远不会执行
  • 你是对的..好吧我改变它,如果我需要一个开放式的非条件返回语句,我如何让它不返回 475?
  • 可以,但是和把return语句移到函数末尾是一样的,你自己选择

标签: ios uitableview heightforrowatindexpath


【解决方案1】:

您只需要将函数顶部的 return 语句移到底部或将其作为当前 if 语句的一部分放在 else 中,因为 return 语句会立即结束函数,因此它下面的代码将永远不会执行。

【讨论】:

    【解决方案2】:

    这样试试

    已编辑

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
    CGFloat height = 475;
    NSLog(@"ISSSS ITTT ONNNN???");
    
    if (currentDescriptionHeight == 16.0) {
        NSLog(@"OHHH so it triggered 1111");
        height = 475;
    }else if (currentDescriptionHeight == 31.5){
        NSLog(@"OHHH so it triggered 2222");
        height = 490.5;
    }else if (currentDescriptionHeight == 47){
        NSLog(@"OHHH so it triggered 3333");
        height = 506;
    }
    
    return height;
    }
    

    【讨论】:

    • 如果三个条件都不为真,这将返回0,而不是所需的475
    • 希望这将返回 475 :-)
    • 现在您只需将height 更改为CGFloat 而不是int
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-18
    • 2012-10-27
    • 2013-10-12
    • 2013-08-21
    • 2021-10-25
    相关资源
    最近更新 更多