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.descriptionLabel.numberOfLines = 0;
[cell.descriptionLabel sizeToFit];
cell.accessoryType = UITableViewCellAccessoryNone;
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;
}
source
share