【发布时间】:2023-12-31 20:49:01
【问题描述】:
我正在尝试使用具有动态高度的单元格创建一个表格视图,一切似乎都工作正常,但后来我注意到了这个奇怪的问题。 当我向下滚动列表并返回时,某些单元格似乎没有完全绘制内容。
正确内容: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;
}
【问题讨论】:
-
我看不到照片..
-
检查照片后 - 奇怪。从来没有遇到过这样的问题。有多少个细胞?我的意思是可能是因为内存泄漏
-
我只创建了 20 个单元格,我已经使用情节提要创建了一个自定义单元格,还没有发生什么事情
-
奇怪的是,它只发生在顶部超出屏幕的单元格上,一旦我滚动列表并且一个单元格到达底部,它就会恢复正常。我认为这可能与heightForRowAtIndexPath有关,当我将静态高度设置为120时,一切正常,但是当我将其设置为100时,我遇到了同样的问题
-
我发现问题是因为我重用了 dequeueReusableCellWithIdentifier 单元格,现在我只是直接从数据数组中抓取文本
标签: ios uitableview cell