2011-01-24 108 views
3

我有一个UITableView,我创建了一个自定义单元格来显示我的表格。我有6个UILables显示,尽管我只有20条记录可以显示,但是当我滚动时它非常慢。UITableView自定义单元格非常慢

这是怎么了我 - 的tableView:的cellForRowAtIndexPath:看起来像:

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

    static NSString *CustomCellIdentifier = @"CustomCellIdentifier"; 

    HistoryCell *cell = (HistoryCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier]; 

    if (cell == nil) { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"HistoryCell" owner:nil options:nil]; 

     for (id oneObject in nib) 
      if ([oneObject isKindOfClass:[UITableViewCell class]]) 
       cell = (HistoryCell *) oneObject; 
    } 

    NSArray *object; 
    object = [cours objectForKey: [NSString stringWithFormat:@"%d", indexPath.section]]; 
    History *rowData = [object objectAtIndex:indexPath.row]; 

    if (rowData.month == 99) { 
     cell.hour.frame = CGRectMake(10, 0, 135, 35); 
     cell.data.hidden = YES; 
     cell.hour.textColor = [UIColor blackColor]; 
     cell.hour.font = [UIFont fontWithName:@"Verdana" size:17]; 
    } else { 
     cell.data.hidden = NO; 
     cell.hour.frame = CGRectMake(10, 16, 135, 19); 
     cell.hour.textColor = [UIColor grayColor]; 
     cell.hour.font = [UIFont fontWithName:@"Verdana" size:12]; 
    } 

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
    [formatter setDateFormat:@"d (EEEE)"]; 
    [formatter setLocale:self.selectedLanguageLocale]; 
    NSString *stringFromDate = [formatter stringFromDate:rowData.data]; 
    [formatter release]; 

    cell.data.text = stringFromDate; 
    cell.hour.text = rowData.ora; 

    float Var1 = [rowData.Var2 floatValue]; 
    float Var2 = [rowData.Var2 floatValue]; 

    cell.R1.text = [self floatToStringFormat: [rowData.R1 floatValue]]; 
    cell.R2.text = [self floatToStringFormat: [rowData.R2 floatValue]]; 

    if (Var1 <= 0) { 
     cell.Var1.textColor = [UIColor greenColor]; 
    } else { 
     cell.Var1.textColor = [UIColor redColor]; 
    } 
    if (Var2 <= 0) { 
     cell.Var2.textColor = [UIColor greenColor]; 
    } else { 
     cell.Var2.textColor = [UIColor redColor]; 
    } 
    cell.Var1.text = [self floatToStringFormat:Var1]; 
    cell.Var2.text = [self floatToStringFormat:Var2]; 

    cell.selectionStyle = UITableViewCellSelectionStyleGray; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

    return cell; 
} 

原因运行上滚动缓慢是因为所有的事情,我在这里做(NSDateFormatter,CGMakeRect,floatToStringFormat ... )或者重复使用单元格是否有问题?

floatToStringFormat是一个函数来数字格式到4位小数:

- (NSString *)floatToStringFormat:(float)number{ 
    NSNumberFormatter *myFloat = [[NSNumberFormatter alloc] init]; 
    [myFloat setFormatterBehavior:NSNumberFormatterBehavior10_4]; 
    [myFloat setNumberStyle:NSNumberFormatterDecimalStyle]; 
    [myFloat setRoundingMode:NSNumberFormatterRoundHalfUp]; 
    [myFloat setMinimumFractionDigits:4]; 
    [myFloat setMaximumFractionDigits:4]; 
    NSString *res = [myFloat stringFromNumber:[NSNumber numberWithFloat:number]]; 
    [myFloat release]; 
    return res; 
} 

回答

6

创建和设置格式化程序对象确实是一个昂贵的操作,因此我会先重新使用格式化程序对象,因为它们在每次函数调用时都是相同的。因此,要么让他们在你的数据源类的静态变量或即时变量和创建方式如下:

//static variable case 
NSDateFormatter *formatter = nil; 
if (!formatter){ 
    formatter = [[NSDateFormatter alloc] init]; 
    [formatter setDateFormat:@"d (EEEE)"]; 
    [formatter setLocale:self.selectedLanguageLocale]; 
} 
NSString *stringFromDate = [formatter stringFromDate:rowData.data]; 
... 
3

首先,使用两个不同的标识符:CustomCellIdentifierBanciHistoryCellIdentifier

其次,你是否真的需要在每次显示一个新的单元格后在NSArray *object;之后做所有事情?因为如果你不这样做,你应该将它移动到if (cell == nil) {区块。

+0

其实真正的是BanciHistoryCellIdentifier,但我已经在这里将它重命名为CustomCellIdentifier,对于你来说这更容易理解,而且这个从重命名中逃脱了。 – CristiC 2011-01-24 18:56:45

2

根据我的经验,如果您有三个或更多子视图(也取决于设备和视图),表格视图单元格的绘制会显着减慢。尝试直接在drawRect中绘制内容:而不是使用子视图,这应该会加快速度。

-1

你有CellIdentifier在界面生成器设置?它必须与您在代码中使用的完全一致。设置一个断点,它从笔尖加载单元格,并确保它在您滚动时重用单元格。

0

你在这里做什么:

if (cell == nil) { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"HistoryCell" owner:nil options:nil]; 

     for (id oneObject in nib) 
      if ([oneObject isKindOfClass:[UITableViewCell class]]) 
       cell = (HistoryCell *) oneObject; 
    } 

去阅读关于如何做到这一点的正确documentation。其次,如果将日期和数字转换为字符串需要太长的时间,则应该存储字符串值,并在需要修改时将其转换为值。