iOS:TableView Not Scrolling Smoothly due to Setting Constraints at Runtime in CellForRowAtIndexPath

250 views Asked by At

I am Using StoryBoard and Using Autolayout , I have set Constraints at runtime for Custom cell.Also I have set constraints in ViewDidLayoutSubviews to Handle Device Orientation. So This is taking time for Cell to configure and my cell is not Scrolling Smoothly .Can anyone help me on this?If I have to not set constraints at runtime then where should I set them?Hope I am Clear.Thanks in advance

2

There are 2 answers

0
Ankit Kumar On

I'm with nburk, it is not possible to solve with this short detail. But as You are using custom cell in tableView. In the cellForRowAtIndexPath method every time the cell is created so you can use dispatch_async(dispatch_get_main_queue(), ^{......your code here for custom cell..... }); //used for updating UI I'm not sure but as your lines it showing the screen UI is not updating on time.

2
sgl0v On

I would suggest you to define a UITableViewCell's subclass and create all constraints in the init/initWithCoder: method.

And don't forget to reuse your cells correctly. In this way you won't re-create your cell's constraints all the time.

EDIT: take a look at the example below.

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