Large scrolling tables

I have a performance issue in my table view

my cellForRow looks like this:

if (tableView == allMonthTable) {

        static NSString *cellIdentifier = @"cell";

        AllMonthCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

        if (cell == nil) {
            cell = [[AllMonthCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier] ;
        }
        Month *mo = [[dataHandler allMonth]objectAtIndex:indexPath.section];
        double budgetValue = mo.budget.doubleValue;
        [cell.budgetLabel setText:[NSString stringWithFormat:@"%.2f",budgetValue ]];
        double spentValue = mo.spent.doubleValue;
        [cell.spentLabel setText:[NSString stringWithFormat:@"%.2f",spentValue ]];
        if (spentValue > 0) {
            [cell.spentLabel setText:[NSString stringWithFormat:@"+%.2f",spentValue]];
            [cell.spentLabel setTextColor:[self greenColor]];    
        }
        else if (spentValue < 0){
            [cell.spentLabel setText:[NSString stringWithFormat:@"%.2f",spentValue]];
            [cell.spentLabel setTextColor:[self redColor]];      
        }
        double balanceValue = budgetValue+spentValue;
        [cell.balanceLabel setText:[NSString stringWithFormat:@"%.2f",balanceValue ]];
        if (balanceValue > 0) {
            [cell.balanceLabel setText:[NSString stringWithFormat:@"+%.2f",balanceValue]];
            [cell.balanceLabel setTextColor:[self greenColor]];    
        }
        else{
            [cell.balanceLabel setText:[NSString stringWithFormat:@"%.2f",balanceValue]];
            [cell.balanceLabel setTextColor:[self redColor]];      
        }
        double avgDayValue = mo.avgDay.doubleValue;
        [cell.avgDayLabel setText:[NSString stringWithFormat:@"%.2f",avgDayValue ]];

        if (avgDayValue > 0) {
            [cell.avgDayLabel setText:[NSString stringWithFormat:@"+%.2f",avgDayValue]];
        }
        int numberOfExpValue = mo.expenses.intValue;
        [cell.numberOfExpLabel setText:[NSString stringWithFormat:@"%d",numberOfExpValue ]];
        return cell;
    }
    return nil;
}

      

and my AllMonthCell.m files look like this:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

        UIFont *font = [UIFont fontWithName:@"Cochin" size:14.0];
        UIFont *fontBold = [UIFont fontWithName:@"Cochin-Bold" size:14.0];


        self.frame = CGRectMake(0, 0, 312, 100);

        UIView *top = [[UIView alloc]initWithFrame:CGRectMake(4, 0, 304, 1)];
        [top setBackgroundColor:[UIColor lightGrayColor]];

        UILabel *budgetStringLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 102, 16)];
        [budgetStringLabel setTextAlignment:UITextAlignmentLeft];
        [budgetStringLabel setFont:font];
        [budgetStringLabel setTextColor:[UIColor lightGrayColor]];
        [budgetStringLabel setText:@"Month Budget:"];
        [budgetStringLabel setBackgroundColor:[self grayColor]];

        UILabel *spentStringLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 30, 102, 16)];
        [spentStringLabel setTextAlignment:UITextAlignmentLeft];
        [spentStringLabel setFont:font];
        [spentStringLabel setTextColor:[UIColor lightGrayColor]];
        [spentStringLabel setText:@"Spent Money:"];
        [spentStringLabel setBackgroundColor:[self grayColor]];

        UIView *divide1 = [[UIView alloc]initWithFrame:CGRectMake(10, 50, 292, 1)];
        [divide1 setBackgroundColor:[UIColor lightGrayColor]];


        UILabel *balanceStringLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 55, 102, 16)];
        [balanceStringLabel setTextAlignment:UITextAlignmentLeft];
        [balanceStringLabel setFont:font];
        [balanceStringLabel setTextColor:[UIColor lightGrayColor]];
        [balanceStringLabel setText:@"Month Balance:"];
        [balanceStringLabel setBackgroundColor:[self grayColor]];

        UIView *divide2 = [[UIView alloc]initWithFrame:CGRectMake(10, 74, 292, 1)];
        UIView *divide3 = [[UIView alloc]initWithFrame:CGRectMake(10, 76, 292, 1)];
        [divide2 setBackgroundColor:[UIColor lightGrayColor]];
        [divide3 setBackgroundColor:[UIColor lightGrayColor]];


        UILabel *avgDayStringLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 94, 200, 16)];
        [avgDayStringLabel setTextAlignment:UITextAlignmentLeft];
        [avgDayStringLabel setFont:font];
        [avgDayStringLabel setTextColor:[UIColor lightGrayColor]];
        [avgDayStringLabel setText:@"Per Day:"];
        [avgDayStringLabel setBackgroundColor:[self grayColor]];

        UILabel *numberOfExpStringLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 114, 102, 16)];
        [numberOfExpStringLabel setTextAlignment:UITextAlignmentLeft];
        [numberOfExpStringLabel setFont:font];
        [numberOfExpStringLabel setTextColor:[UIColor lightGrayColor]];
        [numberOfExpStringLabel setText:@"Expenses Made:"];
        [numberOfExpStringLabel setBackgroundColor:[self grayColor]];

        [self addSubview:budgetStringLabel];
        [self addSubview:top];
        [self addSubview:spentStringLabel];
        [self addSubview:divide1];
        [self addSubview:balanceStringLabel];
        [self addSubview:divide2];
        [self addSubview:divide3];
        [self addSubview:numberOfExpStringLabel];
        [self addSubview:avgDayStringLabel];


        self.budgetLabel = [[UILabel alloc]initWithFrame:CGRectMake(200, 10, 102, 16)];
        [self.budgetLabel setTextAlignment:UITextAlignmentRight];
        [self.budgetLabel setFont:font];
        [self.budgetLabel setBackgroundColor:[self grayColor]];
        [self.budgetLabel setTextColor:[UIColor darkGrayColor]];



        self.spentLabel = [[UILabel alloc]initWithFrame:CGRectMake(200, 30, 102, 16)];
        [self.spentLabel setTextAlignment:UITextAlignmentRight];
        [self.spentLabel setFont:font];
        [self.spentLabel setBackgroundColor:[self grayColor]];
        [self.spentLabel setTextColor:[UIColor lightGrayColor]];



        self.balanceLabel = [[UILabel alloc]initWithFrame:CGRectMake(200, 55, 102, 16)];
        [self.balanceLabel setTextAlignment:UITextAlignmentRight];
        [self.balanceLabel setFont:fontBold];
        [self.balanceLabel setBackgroundColor:[self grayColor]];



        self.avgDayLabel = [[UILabel alloc]initWithFrame:CGRectMake(200, 94, 102, 16)];
        [self.avgDayLabel setFont:font];
        [self.avgDayLabel setBackgroundColor:[self grayColor]];
        [self.avgDayLabel setTextColor:[UIColor lightGrayColor]];
        [self.avgDayLabel setTextAlignment:UITextAlignmentRight];






        self.numberOfExpLabel = [[UILabel alloc]initWithFrame:CGRectMake(200, 114, 102, 16)];
        [self.numberOfExpLabel setTextAlignment:UITextAlignmentRight];
        [self.numberOfExpLabel setFont:font];
        [self.numberOfExpLabel setBackgroundColor:[self grayColor]];
        [self.numberOfExpLabel setTextColor:[UIColor lightGrayColor]];




        [self addSubview:self.budgetLabel];
        [self addSubview:self.spentLabel];
        [self addSubview:self.balanceLabel];
        [self addSubview:self.numberOfExpLabel];
        [self addSubview:self.avgDayLabel];


        [self setBackgroundColor:[self grayColor]];

    }
    return self;
}

      

here is a screenshot of the time profiling while scrolling:

time profile

so I create reusable cells of type AllMonthCell and assign values ​​to them, is there any way to make it faster? I have some pretty serious lags when scrolling through the table.

a hint would be great :)

+3


source to share


1 answer


You have to reuse cells

Let me get the template

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"cell";
    UILabel *noExpense;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier] autorelease];
        //Just the view alone should be created here no assigning of data here
        //let me add just one custom view for easy understanding
        noExpense = [[UILabel alloc]initWithFrame:CGRectMake(8, 20, 296, 16)];
       [noExpense setTextAlignment:UITextAlignmentCenter];
       [noExpense setFont:fontName];
       //[noExpense setText:@"No data available yet."];  Dont do this
       [noExpense setTextColor:[UIColor lightGrayColor]];
       [noExpense setBackgroundColor:tableView.backgroundColor];
       [noExpense setTag:100];  //Must set tag here
       [cell addSubview:noExpense];
    }
    else
    {
        noExpense = [cell viewWithTag:100];
        //You have to initialize your custom views which you created already.
        //Just the view alone should be assigned here no assigning of data here
    }
    //Load all the data into the views here
    [noExpense setText:@"Somedata which you have to set"];
    return cell;
}

      



To learn more about the tableview, see the doc. His bible for understanding tables.

http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7

+3


source







All Articles