Manpreet
Manpreet

Reputation: 23

UITableView scrolling lags even for simple text

I am new in iOS Development and still learning.

I have text data in unicode, where each string is saved into NSArray elements. I already computed the heightForRowAtIndexPath and saved it separately in another array so as to minimize the computations later on. Each string has multiple lines to be displayed inside one cell.

I created a custom cell xib and registered as required. Everything works just fine but the problem is when i scroll, tableView is laggy. It takes a few milliseconds to settle the text and settling appears glitchy and hence the scroll is not as smooth as required as this lag is visible.

Here is the code.

This method is called in viewDidLoad to calculate height for rows.

 -(void) computeHeight 
{

    [array removeAllObjects];
    CGFloat height = 0;
    float high;

    self.customCell = [self.tableView dequeueReusableCellWithIdentifier:customCellIdentifier];

    for(int i = 0; i < self.gurbani.count; i++){
        self.customCell.customCellLabel.attributedText = [[NSMutableAttributedString alloc] initWithString:self.gurbani[i] attributes:normalAttributes];
        [self.customCell.contentView layoutIfNeeded];
        height = [self.customCell.customCellLabel systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;

        high = height+20.0;

        [array insertObject:[NSNumber numberWithFloat:high] atIndex:i];
    }
}

Also, below is shown the cellForRowAtIndexPath method

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Fetch A Cell From Given TableView And Assign It To CustomCell
CustomCell *cell = [self.tableView dequeueReusableCellWithIdentifier:customCellIdentifier];

color = textColors[indexPath.row % 8];

// Customize And Feed The Cell Here
if(indexPath.row == 0 | indexPath.row == 1 | indexPath.row == 41 ) {
    cell.contentView.backgroundColor = color;
    cell.customCellLabel.backgroundColor = color;
    cell.customCellLabel.attributedText = [[NSMutableAttributedString alloc] initWithString:self.gurbani[indexPath.row] attributes:titleAttributes];
    cell.customCellLabel.textColor = [UIColor whiteColor];

}
else {
    cell.contentView.backgroundColor = [UIColor whiteColor];
    cell.customCellLabel.backgroundColor = [UIColor whiteColor];
    cell.customCellLabel.attributedText = [[NSMutableAttributedString alloc] initWithString:self.gurbani[indexPath.row] attributes:normalAttributes];
    cell.customCellLabel.textColor = color;
}
// Return The Completed Cell Here
return cell; }

Finally, in heightForRowAtIndexPath,

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [array[indexPath.row] floatValue]; }

Also, i have specified opaque in cell's contentView and other parts of the tableView.

Also guide me whether using core data could possibly increase scrolling performance. I am open for suggestions.

Upvotes: 1

Views: 246

Answers (2)

Manpreet
Manpreet

Reputation: 23

The only problem was unicode text. It was Punjabi unicode text. I then used a custom font with rewritten text for that font and issue was fixed.

Upvotes: 0

Pankaj
Pankaj

Reputation: 344

You do not need to separately compute the heights of the cells and store it in an array. You can just compute the height of each cell in the heightForRowAtIndexPath method, and get rid of computeHeight method altogether.

Besides this, I see that you are using dequeueReusableCellWithIdentifier inside computeHeight method which is unnecessary. The cell should be populated in cellForRowAtIndexPath method.

So bottomline:

1) Get rid of computeHeight method

2) Move code for calculating cell height to heightForRowAtIndexPath method. Make sure that the correct height is returned for every row.

3) No need to track the instance of the current cell, so get rid of self.customCell. Set the text (self.customCell.customCellLabel.attributedText) in cellForRowAtIndexPath method.

Upvotes: 2

Related Questions