iOS - 动态tableview单元格高度始终返回一个值

时间:2014-05-02 14:22:10

标签: ios objective-c dynamic uitableview

我是iOS开发的新手..而且我一直坚持动态更改表视图高度的问题。

我知道我应该在tableView:heightForRowAtIndexPath:方法中执行此操作,但[cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height内容始终返回一个小值18.0 ..

我的单元格布局如下:

有这样的场景:

以下是 TableViewController 中的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
   MYModel *model = [[MYModel alloc] initWithDictionary:[self.items objectAtIndex:indexPath.row]];
   NSString *reuseIdentfier = @"textCell";
   MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentfier forIndexPath:indexPath];
   [self configureCell:cell forRowAtIndexPath:indexPath withData:model];

   return cell;
}

我正在使用 SDWebImage https://github.com/rs/SDWebImage)来加载图片,以下是配置单元格方法:

- (void)configureCell:(MyTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath withData:(MYModel *)model 
{

   ANOTHERModel *itemCollection = [[ANOTHERModel alloc] initWithDictionary:model.collection];

   cell.collectionTitle.text = [itemCollection title];
   cell.collectionOwner.text = [NSString stringWithFormat:@"by %@", itemCollection.ownerName];
   [cell.collectionOwnerAvatarView setImageWithURL:[NSURL URLWithString:itemCollection.ownerAvatar] placeholderImage:[UIImage imageNamed:@"gravatar.png"]];

   // make circle avatars
   CALayer *layer = [cell.collectionOwnerAvatarView layer];
   [layer setMasksToBounds:YES];
   [layer setCornerRadius:cell.collectionOwnerAvatarView.frame.size.height/2];

   // type
   NSString *imageName = [model.type stringByAppendingFormat:@".png"];
   UIImage *typeImage = [UIImage imageNamed:imageName];
   [cell.typeIconView setImage:typeImage];

   // title and description
   cell.itemTitle.text = [model title];
   cell.itemDescription.text = [model description];

   [cell.itemDescription setTextAlignment:NSTextAlignmentLeft];
   [cell.itemDescription setLineBreakMode:NSLineBreakByWordWrapping];
   [cell.itemDescription setNumberOfLines:0];

   // always returns 18.0
   NSLog(@"%f", [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height);  
}

我真的不明白为什么......请帮帮忙!

谢谢!

0 个答案:

没有答案