2016-01-14 40 views
0

我有一个单元格,其中包含2个多行标签和一个UIImageView。单元具有动态高度,并使用Autolay自动计算。 我的问题是,我在cellForRowAtIndexPath中下载图像:直到那时我不知道图像的高度。在开始的单元格没有正确的布局,我不得不滚动到其他单元格,当我回到我的单元格(它重新绘制),然后布局是正确的。图像下载时如何重新绘制单元格? 下面是我正在使用的代码:表格单元格内的图像块和更改自动布局

NSString static * cellIdentifier = @"titlePostCell"; 

PostTitleTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

if (!cell) { 
    cell = [[PostTitleTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
} 

cell.titleLabel.text = self.entry.title; 
cell.descriptionLabel.text = self.entry.entry_description[@"value"]; 

[cell setNeedsUpdateConstraints]; 


[cell.postImageView pin_setImageFromURL:[[CTImageHelper sharedInstance] resizedImageURLConverterFromStringWithPrefix:self.entry.card_image] completion:^(PINRemoteImageManagerResult *result) { 

    cell.heightSize = [NSNumber numberWithInt:result.image.size.height/2]; 
    cell.heightImageConstraint.constant = cell.heightSize.floatValue; 

}]; 

return cell; 

谢谢!

+0

您需要使单元格自动布局,使单元格的高度应该具有uiimageview高度的相关性。我不认为在块内的cellForRowIndex中给出约束是好方法。 – nikhil84

+0

@ nikhil84单元取决于图像大小和两个标签,因为它们是多行,并且它们没有固定大小 –

回答

0

如果你有一个指针到单元(例如,在你的块),你可以通过下面的代码实现这一点:

NSIndexPath *indexPath = [tableView indexPathForCell:cell]; 
[tableView reloadRowsAtIndexPaths:@[indexPath]]; 

但是,我认为,在一般情况下,调整表视图细胞无用户特别要求你这样做是一个糟糕的用户界面的做法。想象一下,当用户尝试使用它时,用户界面会跳跃!相反,我会在你的细胞中使用固定大小的UIImageView。您可以将任意大小的图像设置为contentModeUIViewContentModeScaleAspectFit,以放入此图像视图。

相关问题