2015-02-07 35 views
5

我使用2個多行標籤製作自定義單元格並將此標籤固定到所有面。當在-tableView:heightForRowAtIndexPath:iOS> = 8我返回UITableViewAutomaticDimension。但是當表格視圖出現時,單元格高度比應該大。在向下滾動之後,向上滾動單元格取正常高度。如何解決這個問題?與UITableViewAutomaticDimension錯誤的UITableViewCell高度

代碼高度:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8) { 
     return UITableViewAutomaticDimension; 
    } 

    static CCTopicTableViewCell *cell; 
    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
     cell = [tableView dequeueReusableCellWithIdentifier:@"topicCellIdentifier"]; 
    }); 
    cell.topic = [self.topics topicAtIndexPath:indexPath]; 

    cell.bounds = CGRectMake(0, 0, CGRectGetWidth(self.tableView.bounds), CGRectGetHeight(cell.bounds)); 

    [cell setNeedsLayout]; 
    [cell layoutIfNeeded]; 

    CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; 
    return size.height + 1; 
} 

而結果:

滾動後:

回答

9

當我不設置的問題消失了tableView的估計的高度適用於iOS 8時設置了多標籤

+0

謝謝,這解決了我的問題。不知道我明白它爲什麼修復它,但我確信在對該主題進行更多研究之後,它會找到我。再次感謝! – 2015-03-27 18:40:20

+0

任何想法_why_這是否有效? – 2017-12-15 21:13:03

1

Automatic dimension的文本,如果該標籤被頂部邊緣底部邊緣有不同的標籤對齊對於一些UILabel都不盡如人意更新preferredMaxLayoutWidth每次。只要確保你沒有這樣的路線。

2

我解決了這個問題通過設置多標籤「無」,「文本」屬性:

override func prepareForReuse() { 
    _label.text = nil 
} 

而且表性的判定「estimatedRowHeight」和「rowHeight的」應正確設置:

_tableView.estimatedRowHeight = 44 
_tableView.rowHeight = UITableViewAutomaticDimension 
+0

太棒了!完美工作(iOS9) – Chris 2016-09-02 12:39:59

相關問題