【问题标题】:Image block inside table view cell and change autolayout表格视图单元格内的图像块并更改自动布局
【发布时间】:2016-01-14 12:55:30
【问题描述】:

我有一个包含 2 个多行标签和一个 UIImageView 的单元格。单元格具有动态高度,并使用自动布局自动计算。 我的问题是我在 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;

谢谢!

【问题讨论】:

  • 您需要对单元格进行自动布局,以便单元格的高度应该依赖于 uiimageview 高度。我不认为在块内的 cellForRowIndex 中给出约束是好方法。
  • @nikhil84 单元格取决于图像大小和两个标签,因为它们是多行且没有固定大小

标签: ios objective-c uitableview autolayout


【解决方案1】:

如果您有一个指向单元格的指针(例如在您的块中),您可以通过以下代码完成此操作:

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

但是,我认为一般来说,在没有用户特别要求的情况下调整表格视图单元格的大小是一种糟糕的 UI 做法。想象一下,当用户尝试使用它时,用户界面会在他们身上跳来跳去!相反,我会在您的单元格中为您的UIImageViews 使用固定大小。您可以通过将contentMode 设置为UIViewContentModeScaleAspectFit 来将任意大小的图像放入这样的图像视图中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-16
    • 1970-01-01
    相关资源
    最近更新 更多