目标C语言 iOS:由于在CellForRowAtIndexPath运行时设置约束,TableView不平滑滚动



我使用故事板和使用自动布局,我在运行时为自定义单元格设置了约束。我还在ViewDidLayoutSubviews中设置了约束来处理设备方向。所以这是需要时间的细胞配置和我的细胞不滚动顺利。有人能帮助我吗?如果我不能在运行时设置约束,那么我应该在哪里设置它们?希望我说的清楚。提前感谢

我是nburk,这是不可能解决这个简短的细节。当你在tableView中使用自定义单元格时。在cellForRowAtIndexPath方法中创建单元格,以便您可以使用设置(dispatch_get_main_queue (), ^{......这里的代码用于自定义单元.....});//用于更新UI我不确定,但你的行显示屏幕UI没有及时更新

我建议你定义一个UITableViewCell的子类,并在init/initWithCoder:方法中创建所有约束。

别忘了正确地重复使用你的细胞。这样你就不用一直重新创建单元格的约束了。

编辑:看看下面的例子。

static NSString* const kCellIdentifier = @"CustomTableViewCellIdentifier";
@implementation CustomTableViewController
- (void)viewDidLoad
{
    [self.tableView registerClass:[CustomTableViewCell class] forCellReuseIdentifier:kCellIdentifier];
}
- (UITableViewCell*) tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    CustomTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
    // configure your cell here
    return cell;
}
@end

@interface CustomTableViewCell: UITableViewCell
@end
@implementation CustomTableViewCell
- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        // add subviews and install constraints here. Don't forget to use contentView instead of cell when you install constraints.
    }
    return self;
}
@end

最新更新