Anand
Anand

Reputation: 10400

UITableView scroll is choppy

Here is my cellForRowAtIndexPath method. Using ARC in my project.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {

    static NSString* cellIdentifier = @"ActivityCell";



    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (!cell) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];

    }



    Activity* activityToShow = [self.allActivities objectAtIndex:indexPath.row];



    //Cell and cell text attributes

    cell.textLabel.text = [activityToShow name];


    //Slowing down the list scroll, I guess...

    LastWeekView* lastWeekView = [[LastWeekView alloc] initWithFrame:CGRectMake(10, 39, 120, 20)];

    [lastWeekView setActivity:activityToShow];

    lastWeekView.backgroundColor = [UIColor clearColor];

    [cell.contentView addSubview:lastWeekView];



     return cell;

}

LastWeelView allocation is slowing down the scroll i guess. In the lastWeekView, I fetch relationships of an entity from CoreData, perform a calculation on those values and draw some colors inside its drawRect method.

Here is the drawRect of LastWeekView

- (void)drawRect:(CGRect)rect
{
    NSArray* activityChain = self.activity.computeChain; //fetches its relationships data


    for (id item in activityChain) {
        if (marking == [NSNull null]) 
        {
            [notmarkedColor set];
        }
        else if([(NSNumber*)marking boolValue] == YES)
        {
            [doneColor set];
        }
        else if([(NSNumber*)marking boolValue] == NO)
        {
            [notdoneColor set];
        }

        rectToFill = CGRectMake(x, y, 10, 10);
        CGContextFillEllipseInRect(context, rectToFill);

        x = x + dx;
    }
}

What can I do to smoothen the scroll of tableView? If I have to asynchronously add this lastWeekView to each cell's contentView, how can i do it? please help.

Upvotes: 0

Views: 218

Answers (1)

Eimantas
Eimantas

Reputation: 49354

I'd suggest allocating LastWeekView in cell's allocation scope. Also - fetch all core data objects in viewDidLoad so that in cellForRowAtIndexPath: method would retrieve it from array and not from the store. It should look something like this:

- (void)viewDidLoad
    ...
    _activities = [Activity fetchAllInContext:managedObjectContext];
    ...
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCell];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
        LastWeekView* lastWeekView = [[LastWeekView alloc] initWithFrame:CGRectMake(10, 39, 120, 20)];

        lastWeekView.backgroundColor = [UIColor clearColor];

        [cell.contentView addSubview:lastWeekView];
    }

    Activity *activityToShow = [_activities objectAtIndex:[indexPath row]];
    LastWeekView *lastWeekView = (LastWeekView *)[[[cell contentView] subviews] lastObject];
    [lastWeekView setActivity:activityToShow];
    return cell;
}

Note that you may also subclass the UITableViewCell to replace contentView with your LastWeekView to quickly access the activity property.

Upvotes: 1

Related Questions