【问题标题】:TableView and heightForRowAtIndexPathTableView 和 heightForRowAtIndexPath
【发布时间】:2012-09-23 11:58:18
【问题描述】:

我希望每个帖子在多行中显示完整标题,并且每个单元格根据文本标题字符数具有自己的自定义高度。我这样做了,但它不起作用。我认为每个对象都应该被独立调用。我怎样才能使这项工作?这是我的代码:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    PFObject *object1 = [PFObject objectWithClassName:@"Posts"];
    NSString *string1 = [object1 objectForKey:@"text"];
    NSLog(@"%@",string1);



    CGSize theSize = [string1 sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(265.0f, 9999.0f) lineBreakMode:UILineBreakModeWordWrap];
    // numberOfTextRows is an integer.

    numberOfTextRows = (round(theSize.height/14));

    if ((indexPath.row== 0) || (numberOfTextRows <2)) {
        return 44;
    }

    else {
        return theSize.height +18;
    }
}

注意:显示的 NSLog 显示 (null) 提前谢谢你..

【问题讨论】:

  • 您是否尝试过从 heightForRowAtIndexPath 方法中删除逻辑并放入固定数字?然后,您可以查看是否是您的逻辑导致了问题。
  • 好吧,如果你的日志显示为null,那么问题不在于你发布的代码,而与object1有关。 object1 是否也为空 - 尝试记录它,看看你得到了什么。
  • @rdelmar object1 的日志是 {}。也许我可以创建一个数组来存储对象的所有值然后调用它们?这行得通吗?
  • 这段代码的另一个问题是string1(如果你能让它工作的话)每次调用这个方法时都是一样的(每行一次)。每次调用该方法时,您应该根据 indexPath 访问不同的字符串。
  • “我应该看哪一页?”我不知道这意味着什么。

标签: objective-c ios xcode height tableview


【解决方案1】:

这是我的解决方案。随心所欲地定制。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellText;
    CGRect screenBound = [[UIScreen mainScreen] bounds];
    CGSize screenSize = screenBound.size;
    CGFloat screenWidth = screenSize.width;
    CGFloat screenHeight = screenSize.height;

    if (searchDisplayController.active && [searchResults count]) {
        cellText = [searchResults objectAtIndex:indexPath.row];
    } else {
        cellText = [detailPeriodsContent objectAtIndex:indexPath.row];
    }
    UIFont *cellFont = [UIFont systemFontOfSize:14];
    CGSize constraintSize = CGSizeMake(screenWidth-40, MAXFLOAT);
    CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

    return labelSize.height + 35;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多