【问题标题】:Xcode - IF statement fails in heightForRowAtIndexPathXcode - IF 语句在 heightForRowAtIndexPath 中失败
【发布时间】:2014-10-26 12:22:48
【问题描述】:

这是我目前拥有的:

在 cellForRowAtIndexPath 中

if ([userSelection  isEqualToString:comparison]){

    changeHeight = NO;

} else if(![userSelection isEqualToString:comparison]) {
    [cell setHidden:YES];
    changeHeight = YES;
}
return cell;

这里是 heightForRowAtIndexPath

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(changeHeight == YES) {
    return 0;
} else return 44;

changeHeight = NO;
}

if 语句是 cellForRowAtIndexPath 完美运行,但 heightForRowAtIndexPath 中的那个不计算并且总是返回 44 - 无论 cellForRowAtIndexPath 中的相等性是否为真。

有什么想法吗?

【问题讨论】:

    标签: iphone xcode uitableview if-statement cell


    【解决方案1】:

    您没有正确地保持状态并且假设heightForRowAtIndexPath 将在cellForRowAtIndexPath 被调用后立即被调用。 可能是真的,但我希望调用的顺序是未定义的,甚至可能不会针对同一个单元格调用。

    因此您需要在每个单元格的基础上保持状态,而不是单个“changeHeight”标志。

    现在我无法为您设计所有这些,但您只需扩展您的模型数据以保存您希望每个单元格的高度。

    还要想想这句话的作用(和你的问题无关,但还是需要整理一下):

    if ([userSelection  isEqualToString:comparison]){
    
    } else if(![userSelection isEqualToString:comparison]) {
    
    }
    

    【讨论】:

    • 谢谢你,我想我明白了。虽然无法弄清楚该声明有什么问题。它可能真的很明显,但我看不到它
    • @user3580512 呵呵,好的,如果x == 'y' 那么就不需要else if x != 'y'。您可以将 else if (...) 替换为 else
    • 天啊,我怎么没看到。我想我需要休息一下!谢谢
    • 实际上,如果您可以在每个委托/数据源方法中测试相同的条件,您可能不需要扩展模型数据。 @Tapas Pal 的答案可能就是您所需要的。
    • 特洛伊木马,非常感谢。我所要做的就是扩展模型,现在它就像一个魅力。
    【解决方案2】:

    基本上heightForRowAtIndexPathcellForRowAtIndexPath 之前被调用。最好像下面那样做。

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return ([userSelection  isEqualToString:comparison])?44.0f:0.0f;
    }
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [cell setHidden:![userSelection  isEqualToString:comparison]];
        /* Your stuff */
    }
    

    【讨论】:

    • 谢谢,我很快就会试一试
    【解决方案3】:

    heightForRowAtIndexPathcellForRowAtIndexPath 之前被调用。因此changeHeight 没有正确的状态。并用您的if...else 思考trojanfoe's 的问题。

    【讨论】:

      猜你喜欢
      • 2018-02-02
      • 2019-01-26
      • 2013-02-28
      • 2018-11-06
      • 2021-10-17
      • 2015-02-24
      • 1970-01-01
      • 1970-01-01
      • 2021-07-19
      相关资源
      最近更新 更多