2012-04-05 5 views
3

i ein Leistungsproblem habe in meinem Tableviewgroßen Tableview Scrollen hinkt

meine cellForRow so aussieht:

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

und meine AllMonthCell.m Dateien wie das aussieht:

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

hier ist ein Screenshot des Zeitprofils beim Scrollen:

time profile

also ich wiederverwendbare Zellen des Typs AllMonthCell erstellen und ihnen die Werte zuweisen, gibt es eine Möglichkeit, es schneller zu machen? Ich habe ziemlich ernst Lags während die Tabelle scrollen ..

ein Hinweis toll :)

+0

Zellen sollten wiederverwendet werden. – NeverBe

+0

Sie verwenden nicht die Wiederverwendung von Zellen. tableView: dequeueReusableCellWithIndetifier: – Vlad

Antwort

3

Sie wieder verwenden die Zellen sollten

Lassen Sie mich leiten Sie ein Muster

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

Um zu wissen, wäre mehr über die Tabellenansicht finden Sie im Dokument. Es ist die Bibel, um Tabellenansichten zu verstehen.

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

+0

Hallo, danke für eine gute Antwort. Ich habe meinen Code für eine Tabelle angepasst und in der Frage aktualisiert. Ich habe eine Klasse für eine benutzerdefinierte Zelle erstellt, zugewiesen, wenn es nicht Null ist, und dann einfach die Werte zu seinen Steckdosen zuweisen - aber ich habe immer noch etwas zurück ..:/ –

+0

siehe neuen Code =) –

Verwandte Themen