【问题标题】:iOS 8 UITableViewCell extra broken separatorsiOS 8 UITableViewCell 额外损坏的分隔符
【发布时间】:2014-09-23 10:09:56
【问题描述】:

好的,我不知道 iOS 8 发生了什么,但是当我在它上面运行我的应用程序时,我看到了这些额外的 UITableViewSeparatorViews:

更新

我使用了 Xcode 6 视图调试器,我看到在这个有问题的 UITableView 中,我看到这些额外的视图被添加为我的 UITableViewCellContentView 的子视图

_UITableViewCellSeparatorView

为什么 iOS 8 将这些随机子视图添加到我的单元格的 contentView 中?

从屏幕截图中可以看出,那些多余的线条是那些没有到达屏幕边缘的线条。

在我的视图控制器中,我使用以下代码使线条一直延伸到屏幕边缘:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }

    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

在我的自定义单元类中,我正在设置:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code

        _reuseID = reuseIdentifier;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [[BSGThemeManager sharedTheme] clearColor];

        self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

        self.containerView = [[UIView alloc] init];

        _photo = [[BSGUserPhotoView alloc] init];
        [_photo clipPhotoToRadius:37.5];
        //_photo.layer.cornerRadius = 37.5 / 2.0;
        //_photo.layer.borderColor = [[BSGThemeManager sharedTheme] photoBorderColor];
        //_photo.layer.borderWidth = [[BSGThemeManager sharedTheme] photoBorderWidth];
        //_photo.clipsToBounds = YES;

        self.photo.alpha = 0;

        _message = [[UILabel alloc] init];
        _message.backgroundColor = [[BSGThemeManager sharedTheme] clearColor];
        _message.numberOfLines = 0;
        _message.lineBreakMode = NSLineBreakByWordWrapping;
        _message.font = [[BSGThemeManager sharedTheme] varelaRoundFontWithSize:15];

        self.message.alpha = 0;

        _time = [[UILabel alloc] init];
        _time.textAlignment = NSTextAlignmentRight;
        _time.font = [[BSGThemeManager sharedTheme] helveticaNeueFontWithSize:16];
        _time.textColor = [[BSGThemeManager sharedTheme] postTimeColor];
        [_time setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
        [_time setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];

        self.time.alpha = 0;

        [self.containerView addSubview:_photo];
        [self.containerView addSubview:_message];
        [self.containerView addSubview:_time];

        [self.contentView addSubview:self.containerView];

        [self addAllConstraints];
    }
    return self;
}

我不确定这条线是否是它的原因:

self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

我的单元格的自动布局代码是:

-(void)addAllConstraints
{
    self.containerView.translatesAutoresizingMaskIntoConstraints = NO;
    self.photo.translatesAutoresizingMaskIntoConstraints = NO;
    self.message.translatesAutoresizingMaskIntoConstraints = NO;
    self.time.translatesAutoresizingMaskIntoConstraints = NO;

    id views = @{
                 @"containerView": self.containerView,
                 @"photo": self.photo,
                 @"message": self.message,
                 @"time": self.time
                 };

    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[containerView]|" options:0 metrics:nil views:views]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[containerView]|" options:0 metrics:nil views:views]];


    // --------------------------------------------------
    // Horizontal Constraints
    // --------------------------------------------------

    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[photo(37.5)]-15-[message(195)]" options:0 metrics:nil views:views]];

    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[time]-10-|" options:0 metrics:nil views:views]];

    // --------------------------------------------------
    // Vertical Constraints
    // --------------------------------------------------

    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[photo(37.5)]" options:0 metrics:nil views:views]];

    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.photo attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];

    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.message attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.photo attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];

    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.time attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.photo attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
}

还有其他人遇到过这个奇怪的 iOS 8 错误吗?

它不会在 iOS 7 上显示。

【问题讨论】:

  • 您的回答对我不起作用。还有其他解决方案吗?
  • 不知道,控制台输出中是否有任何冲突的约束?就我而言,我首先确保我没有任何冲突的约束。也许在您的源代码中发布另一个 Stackoverflow 问题,以便其他人可以看到并希望提供解决方案?
  • 已解决。在下面检查我的答案。谢谢:)

标签: ios uitableview autolayout ios8


【解决方案1】:

我找到了一个简单的解决方案,当我覆盖 layoutSubviews 方法时,我也调用了 super 方法,如下所示:

-(void)layoutSubviews {
[super layoutSubviews];
// rest of custom implementation code here
. . . 
}

希望对你有帮助

【讨论】:

    【解决方案2】:

    将突出显示的内容更改为“0”,然后它就解决了。是通过XIB。

    【讨论】:

    • 问题不在于左边距,问题是我在 iOS 8 中突然发现了这些额外的线条。我已经在上面的代码中将 tableView 的分隔插入设置为 UIEdgeInsetsZero .它在 iOS 7 中运行良好,但在 iOS 8 中却不行。
    【解决方案3】:

    大声笑....好吧...我想我解决了这个问题。

    我的一个子视图称为“消息”,它只是一个 UILabel。

    在我的自定义 tableview 单元格的类中,此代码导致出现额外的行:

    -(void)layoutSubviews
    {
        self.message.preferredMaxLayoutWidth = self.bounds.size.width * 0.75;
    }
    

    在摆脱它之后,这些单元格的行为就像我在 iOS 7 中喜欢的那样。

    【讨论】:

    • 尝试保留它并调用 super,就像在 init 中一样
    【解决方案4】:

    对我来说,自定义 tableViewCells 的动态高度导致了一个问题,具体来说是 heightForRowAtIndexPathestimatedHeightForRowAtIndexPath 委托方法。一个适用于 iOS 7,另一个适用于 iOS 8。通过有条件地检查 iOS 版本解决了这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-21
      • 1970-01-01
      • 1970-01-01
      • 2014-11-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多