0
我正在嘗試實現heightForRowAtIndexPath,並且想使用已佈局的自動佈局原型單元格來查找高度。我已經嘗試了以下所有內容,但都沒有給出單元的實際高度。他們似乎給我的標籤是單詞包裝之前的單元格的高度。獲取包含文本的自動佈局單元格的高度
我知道我可以使用NSString方法來查找每個字符串的高度,並找出單元格的高度,但如果我沿着這條路走,我基本上會重複自動佈局的功能,並且必須維護單元格的佈局在兩個地方。我真的想避免這一點,我假設我在這裏錯過了一些非常簡單的東西。
我的原型單元格的高度在IB中設置爲67,IB中的內容視圖的高度設置爲66。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"in heightForRowAtIndexPath: %@", indexPath);
if (!self.prototypeCell)
{
self.prototypeCell = [self.tableView dequeueReusableCellWithIdentifier:@"MessageCell"];
}
// set the text in the labels
[self configureCell:self.prototypeCell forIndexPath:indexPath isForOffscreenUse:YES];
[self.prototypeCell setNeedsLayout];
[self.prototypeCell layoutIfNeeded];
// this gives 58 regardless of the length of the text in my labels
CGSize contentViewSize = [self.prototypeCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
// this gives me 58.5 regardless of the length of text in my labels
CGSize cellSize = [self.prototypeCell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return cellSize.height;
}
你是如此接近得到它的權利!它可以幫助你看到一個有效的例子。 https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/iOS7bookExamples/bk2ch08p424variableHeights/ch21p722variableHeights/RootViewController.m這是可下載項目的一部分,因此您可以真正看到它的運行。 – matt