UITableViewCell中的UIImageView使用不同的约束进行刷新

时间:2014-09-19 00:06:42

标签: ios uitableview uiimageview autolayout

这个问题让我暂时停留了一段时间,我无法理解为什么会这样。

我有一个包含自定义单元格的tableView。单元格左上角有一个imageView,然后是从plist中查找的各种信息。我正在使用自动布局设置所有元素的位置。我已刷新所有约束,因此它们都已更新。见下文:

enter image description here

当我向下滚动视图并且单元格离开屏幕时,会出现问题。每次他们都会被刷新,我就会把他们叫出去。

BSettingsCell * cell = [tableView_ dequeueReusableCellWithIdentifier:bSettingsCellIdentifier];

当他们确实刷新时,他们受约束的约束现在不起作用。图像视图为60x60像素,图像为120x120像素,名为fruitMainImage@2x.png,可将视网膜屏幕考虑在内。

enter image description here

我在图片周围放了一个红色边框,以便更容易看到发生了什么。正如您所看到的那样,一旦刷新,图像就会因某种原因向下移动。

我已尝试切换到框架布局以将其固定到位,这不起作用。

我发现this stackoverflow问题似乎引发了同样的问题,但只是将其提升为一个问题而不是帮助解决问题。

任何帮助都会非常感激,因为如果有一种简单的方法可以修复它,我不想用keeplayout重写我的所有代码。

修改

这是我的cellForRowAtIndex方法:

- (UITableViewCell *)tableView:(UITableView *)tableView_ cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    BSettingsCell * cell = [tableView_ dequeueReusableCellWithIdentifier:bSettingsCellIdentifier];

    NSDictionary * dict = [_settingsData objectForKey:_settingsPacks[indexPath.row]];

    cell.nameLabel.text = [dict objectForKey:bSettingsName];
    cell.byLabel.text = [dict objectForKey:bArtist];
    cell.stickerImageView.image = [UIImage imageNamed:[dict objectForKey:bImage]];

    // To make it obvious that the view is changing position
    cell.stickerImageView.layer.borderColor = [UIColor redColor].CGColor;
    cell.stickerImageView.layer.borderWidth = 2;

    cell.descriptionView.text = [dict objectForKey:bDescription];

    [cell.linkButton setTitle: [dict objectForKey:bLink] forState: UIControlStateNormal];
    cell.linkButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
}

以及XIB文件中自动布局约束的镜头:(注意虽然未包含在屏幕截图中,但名称和标签都符合约束条件)

enter image description here

2 个答案:

答案 0 :(得分:1)

您是否尝试过使用

BSettingsCell * cell = [tableView_ dequeueReusableCellWithIdentifier:bSettingsCellIdentifier forIndexPath: indexPath];

代替?

答案 1 :(得分:0)

虽然解决方案非常平凡,但它确实帮助了我,所以我会在这里发布。

基本上,解决方案是删除我所做的事情,并在完成每个步骤时添加它。虽然最后它看起来完全一样有效。我认为有时会出现进入Xcode并导致问题的错误。通常只是重新设置工作将解决这些问题。

当试图解决这个问题时,我假设错误是在我的代码中的某个地方运行,我可能花了太长时间试图找到错误。最后花几分钟重做它a)经常让你看到错误,因为你可以看到你犯了错误的步骤b)它可以重置由Xcode引起的任何错误。

希望这可以帮助任何有类似挫折感的人