【问题标题】:Avoid animation of UITableViewCell contents, when using AutoLayout and animating size of UITableView在使用 AutoLayout 和 UITableView 的动画大小时,避免 UITableViewCell 内容的动画
【发布时间】:2015-03-15 04:11:57
【问题描述】:

最初我有一个 UITableView,它只占用屏幕的一小部分,使用 AutoLayout 约束。 UITableViewCells 布局很好——它们非常基本,只有一个橙色的左对齐标签和一个黑色的右对齐标签。

现在,如果我在 UITableView 上向上滑动,它会进行动画处理(AutoLayout 约束),以占据屏幕的大部分,同时重新加载表格并将额外的 UITableViewCells 带入场景。

我的问题是,新 UITableViewCells 的布局与 UITableView 的大小调整一起动画。黑色右标签出现在橙色标签之后,并向右移动。最后,一切都按预期对齐,只是我不明白为什么当单元格出现在屏幕上时,黑色右标签不是简单地定位在最右边,而是动画到位。

有没有办法告诉 UITableViewCell 根据 UITableView 的宽度和指定的 AutoLayout 约束立即布局其内容,从而绕过 UITableViewCell 内容的动画?

我创建 UITableViewCell 的代码如下所示:

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.textLabel = [[UILabel alloc]init];
        self.textLabel.translatesAutoresizingMaskIntoConstraints = NO;
        self.textLabel.font = [UIFont boldSystemFontOfSize:16.0];
        self.textLabel.textColor = [UIColor orangeColor];
        [self.contentView addSubview:self.textLabel];

        self.amountLabel = [[UILabel alloc]init];
        self.amountLabel.translatesAutoresizingMaskIntoConstraints = NO;
        self.amountLabel.textAlignment = NSTextAlignmentRight;
        self.amountLabel.font = [UIFont boldSystemFontOfSize:16.0];
        [self.contentView addSubview:self.amountLabel];


        NSDictionary *views = @{@"textLabel": self.textLabel, @"amountLabel": self.amountLabel};
        [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-10-[textLabel]-10-[amountLabel]-10-|" options:0 metrics:nil views:views]];
    }
    return self;
}

动画前的初始TableView:

动画期间的TableView(在它向右的旅程中间带有黑色右标签):

【问题讨论】:

  • 你有没有想过这个问题?我正在与同样的动画问题作斗争。
  • 我知道我来晚了。您是否尝试在将标签添加到 contentView 之前设置它们的框架?像这样的东西:CGRect bounds = self.bounds; CGFloat labelWidth = (bounds.size.width - 10 * 3) / 2; self.textLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, labelWidth, bounds.size.height)]; self.amountLabel = [[UILabel alloc] initWithFrame:CGRectMake(10 + labelWidth + 10, 0, labelWidth, bounds.size.height)];.

标签: uitableview animation ios8 autolayout


【解决方案1】:

以下代码对我很有效。我在 detailTextLabel 中添加了第二个标签,并且在 TableView 的属性检查器中我选择了样式为“Right Detail”。我附上了带有属性检查器的表格视图的屏幕截图。

 cell.textLabel!.text = arrayLabel[indexPath.row]
 cell.detailTextLabel!.text = cntarray[indexPath.row]

希望对你有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-12
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多