Dynamic table cell height (content disabled)

I am trying to create a table view with cells with dynamic heights, everything seemed to work fine, but then I noticed this strange problem. When I scroll down the list and go back, some of the cells do not seem to fully draw the contents.

Correct Content: https://www.dropbox.com/s/eqsx4p6dmsofrko/Screen%20Shot%202012-05-03%20at%2010.30.11%20AM.png

Content reduction: https://www.dropbox.com/s/qqelftkc5jzetk5/Screen%20Shot%202012-05-03%20at%2010.30.19%20AM.png

I create all my cells here:

- (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;
}

And use this to change the height of the cells:

- (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
source share
3 answers

, .

0

[self setNeedsDisplay], ?

0

I don't know why this code works, but you never create a cell in your method tableView:cellForRowAtIndexPath:. I suspect this was omitted in your question.

Move the line [cell.descriptionLabel sizeToFit];from yours tableView:cellForRowAtIndexPath:to the next line

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

at tableView:heightForRowAtIndexPath:

and then call

[cell setNeedsLayout];
[cell setNeedsDisplay];

Then.

0
source

All Articles