【问题标题】:iOS:TableView Not Scrolling Smoothly due to Setting Constraints at Runtime in CellForRowAtIndexPathiOS:由于在运行时在 CellForRowAtIndexPath 中设置约束,TableView 无法平滑滚动
【发布时间】:2015-08-20 00:34:27
【问题描述】:

我正在使用 StoryBoard 和 Autolayout ,我在运行时为自定义单元格设置了约束。我还在 ViewDidLayoutSubviews 中设置了约束以处理设备方向。所以这需要时间来配置单元格并且我的单元格不能平滑滚动。任何人都可以帮我解决这个问题吗?如果我必须在运行时不设置约束,那么我应该在哪里设置它们?希望我很清楚。提前谢谢

【问题讨论】:

  • 由于信息有限,无法确定您应用中的潜在问题...您必须提供更多背景信息,否则将很难提供帮助
  • 发布关于设置约束和 CellForRowAtIndexPath 的代码

标签: ios objective-c uitableview ios-autolayout viewdidlayoutsubviews


【解决方案1】:

我和 nburk 在一起,这个简短的细节无法解决。但是当您在 tableView 中使用自定义单元格时。每次创建单元格时,在 cellForRowAtIndexPath 方法中,您可以使用 dispatch_async(dispatch_get_main_queue(), ^{......你的代码在这里用于自定义单元.....}); //用于更新UI 我不确定,但由于您的线条显示屏幕 UI 没有按时更新。

【讨论】:

    【解决方案2】:

    我建议你定义一个 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
    

    【讨论】:

    • 一些带有解释的例子会很棒。
    • 也提供了示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-29
    • 1970-01-01
    • 2016-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多