2012-05-03 101 views
3

我試圖創建一個表格視圖,其中包含動態高度的單元格,一切似乎都正常,但後來我注意到了這個奇怪的問題。 當我向下滾動列表並返回時,某些單元格似乎不能完全繪製內容。動態表格單元格高度(內容截取)

正確的內容: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; 
} 
+0

我看不到照片.. – TheNavigat

+0

對不起,應該現在修復 –

+0

*檢查照片後* - 奇怪。從來沒有這樣的問題。那裏有多少個細胞?我的意思是,也許這是因爲內存泄漏 – TheNavigat

回答

0

解決方案是從數據供稿中抓取文本而不是使用出隊單元。

0

你有沒有試過調用[self setNeedsDisp放置]重新繪製表視圖?

+0

我在表和單元格上都調用它,但似乎沒有幫助 –

0

不知道爲什麼這段代碼可以工作,但是您從來沒有在tableView:cellForRowAtIndexPath:方法中創建單元格。我懷疑你的問題被忽略了。

tableView:cellForRowAtIndexPath:招行[cell.descriptionLabel sizeToFit];至這條線以上

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

tableView:heightForRowAtIndexPath:

,然後調用

[cell setNeedsLayout]; 
[cell setNeedsDisplay]; 

之後。

+0

我正在使用原型單元格的故事板並重新使用該單元格。即使沒有sizeToFit它也有同樣的問題。 一旦一個單元格在顯示器上方移動,它就會切掉部分內容。 –