gpt4 book ai didi

iphone - UITableView自定义单元格非常慢

转载 作者:行者123 更新时间:2023-12-03 19:31:16 25 4
gpt4 key购买 nike

我有一个 UITableView,并且创建了一个自定义单元格来显示我的表格。我显示了 6 个 UILable,虽然我只有 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];
...

关于iphone - UITableView自定义单元格非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4785806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com