2012-04-05 118 views
3

我有一個性能問題在我的tableview大的tableview滾動滯後

我cellForRow看起來像這樣:

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; 
} 

和我AllMonthCell.m文件看起來像這樣:

- (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; 
} 

這裏是滾動時間分析的屏幕截圖:

time profile

因此,我創建AllMonthCell類型的可重用單元,並將值分配給它們,有沒有辦法讓它更快?我有相當嚴重的滯後,同時滾動表..

的提示將是巨大的:)

+0

細胞應重新使用。 – NeverBe 2012-04-05 11:24:43

+0

您沒有使用單元重用。 tableView:dequeueReusableCellWithIndetifier: – Vlad 2012-04-05 11:30:15

回答

3

你應該重用細胞

讓我得到你的模式

- (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; 
} 

要知道有關tableview的更多信息,請參閱文檔。它是理解桌面視圖的聖經。

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

+0

嗨, 感謝您的一個很好的答案。我已經調整了我的代碼,並在問題中更新了它。我爲一個定製單元做了一個類,如果它不是零,則分配它,然後將這些值分配給它的出口 - 但仍然有一些滯後..:/ – 2012-04-05 12:16:33

+0

看到新的代碼=) – 2012-04-05 12:19:39