Iphone UITableView自定义单元格速度非常慢
我有一个UITableView,并创建了一个自定义单元格来显示我的表格。我显示了6个UILables,虽然我只有20条记录要显示,但滚动时速度非常慢 这就是my-tableView:cellforrowatinexpath:的外观:Iphone UITableView自定义单元格速度非常慢,iphone,uitableview,custom-cell,Iphone,Uitableview,Custom Cell,我有一个UITableView,并创建了一个自定义单元格来显示我的表格。我显示了6个UILables,虽然我只有20条记录要显示,但滚动时速度非常慢 这就是my-tableView:cellforrowatinexpath:的外观: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSStrin
- (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;
}
首先,您使用了两种不同的标识符:
CustomCellIdentifier
和BanciHistoryCellIdentifier
其次,您真的需要在
NSArray*对象之后做所有事情吗代码>每次显示新单元格时?因为如果不这样做,则应将其移动到if(cell==nil){
块。根据我的经验,如果有三个或更多子视图,则表视图单元格的绘制速度会显著减慢(但也取决于设备和视图)。尝试直接在drawRect中绘制内容:而不是使用子视图,这应该会加快速度。创建和设置格式化程序对象确实是一项昂贵的操作,因此我将从重用格式化程序对象开始,因为它们在每个函数调用中都是相同的。因此,在d中创建静态变量或即时变量ata源类,并按以下方式创建:
//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];
...
您是否在Interface builder中设置了CellIdentifier?它必须与您在代码中使用的完全匹配。请在从nib加载单元格的位置设置断点,并确保在您滚动时重复使用单元格。您在这里做什么:
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;
}
请阅读有关如何正确执行此操作的说明。其次,如果将日期和数字转换为字符串花费的时间太长,请存储字符串值,并在需要修改时将其转换为值。实际上,真正的名称是BanciHistoryCellIdentifier,但我已在此处将其重命名为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;
}