我正在为 iPhone 开发一个 iOS 应用程序,并且有一个 UITableView 和具有可变高度的自定义单元格。对于我使用Masonry的单元格布局:https ://github.com/Masonry/Masonry 。一切都按照我的喜好呈现,我唯一的问题是当一个新的单元格出现在屏幕上时会有一些延迟,这足以严重阻碍用户体验。我对执行时间进行了一些测试,似乎这种滞后的大部分都在函数内部:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
在 iPhone 5 上执行时间约为十分之一秒。此外,我发现大部分时间都发生在函数中:
-(void)updateConstraints;
我的自定义 UITableViewCell 类。在这个函数中,我使用 Masonry 在单元格中设置各种视图约束。所以我基本上把这个滞后缩小到了砌体电话。如果您想知道,整个函数如下所示:
-(void)updateConstraints
{
[super updateConstraints];
[self.thumbnailImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.contentView.mas_top).offset(5);
make.left.equalTo(self.contentView.mas_left).offset(5);
make.right.equalTo(self.contentView.mas_right).offset(-5);
make.height.equalTo(@(self.thumbnailImageView.frame.size.width)).with.priorityHigh();
}];
[self.likeButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.thumbnailImageView.mas_bottom).offset(5);
make.left.equalTo(self.captionTextView.mas_right);
}];
[self.dislikeButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.thumbnailImageView.mas_bottom).offset(5);
make.left.equalTo(self.likeButton.mas_right);
make.right.equalTo(self.contentView.mas_right).offset(5);
}];
[self.captionTextView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.thumbnailImageView.mas_bottom).offset(5);
make.left.equalTo(self.contentView.mas_left);
make.right.equalTo(self.likeButton.mas_left);
make.height.equalTo(@([self captionHeight]));
}];
[self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.greaterThanOrEqualTo(self.captionTextView.mas_bottom).offset(5).with.priorityHigh();
make.bottom.equalTo(self.contentView.mas_bottom);
make.left.equalTo(self.contentView.mas_left).offset(5);
}];
[self.scoreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.captionTextView.mas_right);
make.top.equalTo(self.likeButton.mas_bottom);
make.right.equalTo(self.contentView.mas_right);
}];
[self.numCommentsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.greaterThanOrEqualTo(self.scoreLabel.mas_top);
make.bottom.equalTo(self.contentView.mas_bottom);
make.right.equalTo(self.contentView.mas_right);
}];
}
我想知道是否有任何方法可以减少执行时间并消除这种明显的滞后。我为每个单元格设置的约束与每个单元格的约束完全相同,但有一些例外。这只是 AutoLayout 的问题还是我正在做的事情非常错误?总的来说,这只是一个不好的方法吗?