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