UitaiteView自定义单元非常慢
-
24-10-2019 - |
题
我有一个Uitable View,并且创建了一个自定义单元格以显示我的表格。我显示了6个Uilables,尽管我只有20个记录可显示,但滚动时的速度非常慢。
这就是我的 - 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;
}
解决方案
创建和设置格式化对象确实是一个昂贵的操作,因此我首先重复使用您的格式化对象,因为它们在每个函数调用上都是相同的。因此,要么在数据源类中使其成为静态变量或即时变量,并创建以下方式:
//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];
...
其他提示
首先,您使用两个不同的标识符: CustomCellIdentifier
和 BanciHistoryCellIdentifier
.
其次,您真的需要做所有事情 NSArray *object;
每次显示新单元格时?因为如果不这样做,则应将其移至 if (cell == nil) {
堵塞。
根据我的经验,如果您有三个或更多个子视图,则表视图单元格的绘制会大大减慢(虽然还取决于设备和视图)。尝试直接绘制DrawRect中的内容:而不是使用子视图,这应该加快速度。
你在这里做什么:
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;
}
去阅读 文档 关于如何正确执行此操作。其次,如果这花费了太长时间以将日期和数字转换为字符串,然后将字符串值存储,然后在需要修改它们时将其转换为值。
您是否在接口构建器中设置了单元格式?它必须完全匹配您在代码中使用的内容。设置一个断点,从笔尖加载单元格,并确保滚动时它重复使用单元格。
不隶属于 StackOverflow