Cri*_*tiC 3 iphone uitableview custom-cell
我有一个UITableView,我已经创建了一个自定义单元格来显示我的表格.我显示了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;
}
Run Code Online (Sandbox Code Playgroud)
在滚动上运行缓慢的原因是因为我在这里做的所有事情(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;
}
Run Code Online (Sandbox Code Playgroud)
创建和设置格式化程序对象确实是一项昂贵的操作,因此我首先重新使用格式化程序对象,因为它们在每个函数调用中都是相同的.因此,要么在数据源类中创建静态变量或即时变量,请创建以下方法:
//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];
...
Run Code Online (Sandbox Code Playgroud)