我有一个像UITableViewCell这样的Twitter,它显示用户图像,用户名,推文以及回复,转发和收藏的按钮。我经历了几个教会如何在ios中自动布局表格的网站。这个有道理Table Layout Blog,但我不能让它发挥作用。我知道我需要使用两种方法heightForRowAtIndexPath和cellForRowAtIndexPath。另外,我有一个configureCell:cell,用数据填充单元格。这是我基于博客的实现。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"TweetCell";
TweetCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[self configureCell:cell forIndexPath:indexPath isForOffscreenUse:NO];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (!self.prototypeCell)
{
self.prototypeCell = [self.tableView dequeueReusableCellWithIdentifier:@"TweetCell"];
}
[self configureCell:self.prototypeCell forIndexPath:indexPath isForOffscreenUse:YES];
[self.prototypeCell layoutIfNeeded];
CGSize size = [self.prototypeCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
NSLog(@"%f",size.height);
return size.height;
}
-(void) configureCell:(TweetCell*)cell forIndexPath:(NSIndexPath*)indexPath isForOffscreenUse:(BOOL)isForOffscreenUse{
Tweet *tweet = self.tweets[indexPath.row];
cell.tweetText.text = tweet.text;
NSURL *url = [NSURL URLWithString:[tweet imageURLFromTweet]];
[cell.tweetImageView setImageWithURL:url];
cell.tweetImageView.contentMode = UIViewContentModeScaleAspectFit;
cell.tweetUserName.text = [tweet usernameFromTweet];
}
问题是heightForRowAtIndexPath size.height为0.有人可以帮忙吗?感谢。
答案 0 :(得分:0)
在heightForRowAtIndexPath
,将不会创建单元格。您需要独立于单元格来测量内容的高度。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
Tweet *tweet = self.tweets[indexPath.row];
CGFloat height = [tweet.text sizeWithFont:[Your Font] constrainedToSize:[CGSizeMake(Your Cell Width, CGFLOAT_MAX)];
NSURL *url = [NSURL URLWithString:[tweet imageURLFromTweet]];
UIImage *image = [Get your image and cache it so you can the set it during configureCell];
height += image.size.height;
return height;
}