简体   繁体   中英

UITableview lags when scrolling.

I'm trying to implement events table like in iOS 6 calendar.

I have UITableView with 500 rows(events). When I start scrolling animation is smooth.

But after two hundred rows begins lags. In native iOS 6 calendar events list all work smoothly.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    double begin = CFAbsoluteTimeGetCurrent();
    if(!self.isNoResults)
    {
        NSString *cellIdentifier = [NSString stringWithFormat: @"Cell %i %i", indexPath.row, indexPath.section];
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        UIImageView *circleImgView;
        NSString *startTimeStr;
        NSDateFormatter *formatter;
        UILabel *timeLbl;
        UILabel *titleLbl;
        UILabel *endsLbl;
        UIImageView *eventTypeImgView;
        if(cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            circleImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"newMesIcon.png"]];
            circleImgView.frame = CGRectMake(14, 19, 12, 12);
            [cell.contentView addSubview:circleImgView];



            timeLbl = [[UILabel alloc] init];
            [timeLbl setBackgroundColor:[UIColor clearColor]];
            timeLbl.frame = CGRectMake(35, 15, 200, 20);
            timeLbl.font = [UIFont systemFontOfSize:13];
            timeLbl.tag = 333;
            [cell.contentView addSubview:timeLbl];

            titleLbl = [[UILabel alloc] init];
            titleLbl.frame = CGRectMake(123, 15, 200, 20);
            titleLbl.font = [UIFont fontWithName:@"Arial-BoldMT" size:18];
            [titleLbl setBackgroundColor:[UIColor clearColor]];
            titleLbl.tag = 444;
            [cell.contentView addSubview:titleLbl];
            [cell.contentView setBackgroundColor:[UIColor colorWithRed:243 / 255.0 green:245 / 255.0 blue:247 / 255.0 alpha:1.0]];

            endsLbl = [[UILabel alloc] init];
            [endsLbl setBackgroundColor:[UIColor clearColor]];
            endsLbl.frame = CGRectMake(49, 11, 40, 8);
            [endsLbl setFont:[UIFont fontWithName:@"Arial-BoldMT" size:7]];
            [endsLbl setTextColor:[UIColor redColor]];
            endsLbl.tag = 555;
            [cell.contentView addSubview:endsLbl];

            eventTypeImgView = [[UIImageView alloc] init];
            eventTypeImgView.frame = CGRectMake(95, 16, 16, 17);
            eventTypeImgView.tag = 666;
            [cell.contentView addSubview:eventTypeImgView];
        }


        startTimeStr = [[self.sortedEventsDict[[[[self.sortedEventsDict allKeys] sortedArrayUsingSelector:@selector(compare:)] objectAtIndex:indexPath.section]]  objectAtIndex:indexPath.row] valueForKey:@"starts"];

        formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyy-MM-dd HH:mm"];
        NSDate *startDate = [formatter dateFromString:startTimeStr];
        NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
        [timeFormatter setDateFormat:@"HH:mm"];
        NSString *timeStr = [timeFormatter stringFromDate:startDate];


        NSString *endTimeStr = [[self.sortedEventsDict[[[[self.sortedEventsDict allKeys] sortedArrayUsingSelector:@selector(compare:)] objectAtIndex:indexPath.section]]  objectAtIndex:indexPath.row] valueForKey:@"ends"];
        NSDateFormatter *endformatter = [[NSDateFormatter alloc] init];
        [endformatter setDateFormat:@"yyy-MM-dd HH:mm"];
        NSDate *endDate = [endformatter dateFromString:endTimeStr];
        NSDateFormatter *endtimeFormatter = [[NSDateFormatter alloc] init];
        [endtimeFormatter setDateFormat:@"HH:mm"];
        NSString *endTime = [endtimeFormatter stringFromDate:endDate];


        if([[[self.sortedEventsDict[[[[self.sortedEventsDict allKeys] sortedArrayUsingSelector:@selector(compare:)] objectAtIndex:indexPath.section]]  objectAtIndex:indexPath.row] valueForKey:@"type"] isEqualToString:@"event"])
        {
            ((UIImageView *)[cell.contentView viewWithTag:666]).image = [UIImage imageNamed:@"eventsIcon.png"];
        }
        else
        {
            ((UIImageView *)[cell.contentView viewWithTag:666]).image = [UIImage imageNamed:@"appointmentsIcon.png"];
        }

        ((UILabel *)[cell.contentView viewWithTag:555]).text = @"";
        if([timeStr isEqualToString:@"00:00"] && [endTime isEqualToString:@"23:59"])
        {
            timeStr = @"all-day";

        }

        else if([timeStr isEqualToString:@"00:00"] && ![endTime isEqualToString:@"23:59"])
        {
            timeStr = endTime;

            ((UILabel *)[cell.contentView viewWithTag:555]).text = @"ENDS";

        }


        ((UILabel *)[cell.contentView viewWithTag:333]).text = timeStr;
        ((UILabel *)[cell.contentView viewWithTag:444]).text = [[self.sortedEventsDict[[[[self.sortedEventsDict allKeys] sortedArrayUsingSelector:@selector(compare:)] objectAtIndex:indexPath.section]]  objectAtIndex:indexPath.row] valueForKey:@"title"];

        startDate = nil;
        timeFormatter = nil;
        timeStr = nil;
        endTimeStr = nil;
        endformatter = nil;
        endDate = nil;
        endtimeFormatter = nil;
        endTime = nil;
        startTimeStr = nil;
        formatter = nil;
        [cell setBackgroundColor:[UIColor colorWithRed:243 / 255.0 green:245 / 255.0 blue:247 / 255.0 alpha:1.0]];
        double finish = CFAbsoluteTimeGetCurrent();
        NSLog(@"TIME DIFF = %f", finish - begin);
        return cell;
    }
    else
    {
        NSString *cellIdentifier = [NSString stringWithFormat: @"Cell No Results %i %i", indexPath.row, indexPath.section];
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if(cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            if(indexPath.row == 2)
            {
                UILabel *noResultsLbl = [[UILabel alloc] init];
                noResultsLbl.text = self.noResultsString;
                [noResultsLbl setFont:[UIFont fontWithName:@"Arial-BoldMT" size:20]];
                [noResultsLbl setTextColor:[UIColor colorWithRed:204 / 255.0 green:204 / 255.0 blue:204 / 255.0 alpha:1.0]];
                noResultsLbl.frame = CGRectMake(60, 15, 200, 20);
                noResultsLbl.textAlignment = NSTextAlignmentCenter;
                [cell.contentView addSubview:noResultsLbl];
            }

        }
        [cell.contentView setBackgroundColor:[UIColor colorWithRed:243 / 255.0 green:245 / 255.0 blue:247 / 255.0 alpha:1.0]];
        cell.userInteractionEnabled = NO;
        double finish = CFAbsoluteTimeGetCurrent();
        NSLog(@"TIME DIFF = %f", finish - begin);
        return cell;
    }
}

Please, help me.

Your cellForRowAtIndexPath is 140 lines long, that is the reason its not running smoothly. I would recommend going over the code and trying to refactor out as much as you can. Formatters are incredibly expensive, and you're alloc/initing several. Make a prototype or xib based cell rather than setting it up programmatically every single time. Id also recommend against using tags for views, put that into a xib or prototype cell and make it an outlet.

Those changes will speed up your scrolling.

Check the size of the images you are using in the cell. I had this problem once when the images were larger than they needed to be. Try reducing the image sizes to their optimal size for the table.

You sort all your data for each cell.

if([[[self.sortedEventsDict[[[[self.sortedEventsDict allKeys] sortedArrayUsingSelector:@selector(compare:)] objectAtIndex:indexPath.section]]  objectAtIndex:indexPath.row] valueForKey:@"type"] isEqualToString:@"event"])

Try to use prepared data for each [table reloadData] call. Basically you could perform all calculations in one place, and then just fill cells with results.

And best way to find what goes wrong is to use Profiler. It will show you which part of code eats all your cpu between frames.

No cell reuse, several alloc/init-s and a lot of NSDateFormatter's with is really expensive

Todo:

  1. Subclass UITableViewCell and make your custom cell
  2. Reuse cell using a constant identifier
  3. Reuse NSDateFormatter s with DFDateFormatterFactory or manually use NSDateFormatter separated by format, because each format is like a new formatter

Reuse cell:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath      *)indexPath
{
     static NSString *CellIdentifier = @"cell";
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (!cell) {
// Setup new cell and constant elements of cell
    }

// Setup data like images, strings and stuff with minimal calculations and formatting
     return cell;
}

My solution might be for a very rare case but it for sure helps.

I had a cell which contained a label with string of currency rubles "₽" (i think this also might be caused by other currency chars). This string was added to the label dynamically every time cell was reused. And the table view was very laggy. Then I removed only Ruble sign and lagging disappeared.

Solution: create a ready label and input the char into the label from the storyboard.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM