动态表格单元格高度(内容截止)

时间:2012-05-03 02:36:38

标签: ios uitableview cell

我正在尝试使用具有动态高度的单元格创建一个表格视图,一切似乎工作正常,但后来我注意到这个奇怪的问题。 当我向下滚动列表并返回时,某些单元格似乎没有完全绘制内容。

正确内容:https://www.dropbox.com/s/eqsx4p6dmsofrko/Screen%20Shot%202012-05-03%20at%2010.30.11%20AM.png

内容剪切:https://www.dropbox.com/s/qqelftkc5jzetk5/Screen%20Shot%202012-05-03%20at%2010.30.19%20AM.png

我在这里创建了所有细胞:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ItemCell";
    ItemCell *cell = (ItemCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    Item *item = [self.activity objectAtIndex:indexPath.row];
    cell.projectLabel.text = item.project;
    cell.descriptionLabel.text = item.description;
    cell.timeLabel.text = item.time;
    cell.timeAgoLabel.text = item.timeAgo;
    //cell.avatar = [UIImageView item.avatar];

    cell.descriptionLabel.numberOfLines = 0;
    [cell.descriptionLabel sizeToFit];

    // remove the right arrow
    cell.accessoryType = UITableViewCellAccessoryNone;

    //[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation: UITableViewRowAnimationNone];

    return cell;
}

并使用它来改变细胞的高度:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ItemCell";
    ItemCell *cell = (ItemCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    CGSize descriptionHeight = [cell.descriptionLabel.text sizeWithFont:[UIFont boldSystemFontOfSize:18.0f] constrainedToSize:CGSizeMake(265.0f, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap];

    NSInteger height = cell.projectLabel.frame.origin.y + cell.projectLabel.frame.size.height;
    height += descriptionHeight.height + 30;

    NSLog(@"height: %d", height);

    return height;
}

3 个答案:

答案 0 :(得分:0)

你试过调用[self setNeedsDisplay]来重绘表视图吗?

答案 1 :(得分:0)

不知道为什么此代码有效,但您永远不会在tableView:cellForRowAtIndexPath:方法中创建单元格。我怀疑你的问题中遗漏了它。

[cell.descriptionLabel sizeToFit];tableView:cellForRowAtIndexPath:移到此行

之上
CGSize descriptionHeight = [cell.descriptionLabel.text sizeWithFont:[UIFont boldSystemFontOfSize:18.0f] constrainedToSize:CGSizeMake(265.0f, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap];

tableView:heightForRowAtIndexPath:

然后致电

[cell setNeedsLayout];
[cell setNeedsDisplay];

之后。

答案 2 :(得分:0)

解决方案是从数据馈送中获取文本,而不是使用出列的单元格。