我的tableview中存在性能问题
我的cellForRow看起来像这样:
if (tableView == allMonthTable) {
static NSString *cellIdentifier = @"cell";
AllMonthCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[AllMonthCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier] ;
}
Month *mo = [[dataHandler allMonth]objectAtIndex:indexPath.section];
double budgetValue = mo.budget.doubleValue;
[cell.budgetLabel setText:[NSString stringWithFormat:@"%.2f",budgetValue ]];
double spentValue = mo.spent.doubleValue;
[cell.spentLabel setText:[NSString stringWithFormat:@"%.2f",spentValue ]];
if (spentValue > 0) {
[cell.spentLabel setText:[NSString stringWithFormat:@"+%.2f",spentValue]];
[cell.spentLabel setTextColor:[self greenColor]];
}
else if (spentValue < 0){
[cell.spentLabel setText:[NSString stringWithFormat:@"%.2f",spentValue]];
[cell.spentLabel setTextColor:[self redColor]];
}
double balanceValue = budgetValue+spentValue;
[cell.balanceLabel setText:[NSString stringWithFormat:@"%.2f",balanceValue ]];
if (balanceValue > 0) {
[cell.balanceLabel setText:[NSString stringWithFormat:@"+%.2f",balanceValue]];
[cell.balanceLabel setTextColor:[self greenColor]];
}
else{
[cell.balanceLabel setText:[NSString stringWithFormat:@"%.2f",balanceValue]];
[cell.balanceLabel setTextColor:[self redColor]];
}
double avgDayValue = mo.avgDay.doubleValue;
[cell.avgDayLabel setText:[NSString stringWithFormat:@"%.2f",avgDayValue ]];
if (avgDayValue > 0) {
[cell.avgDayLabel setText:[NSString stringWithFormat:@"+%.2f",avgDayValue]];
}
int numberOfExpValue = mo.expenses.intValue;
[cell.numberOfExpLabel setText:[NSString stringWithFormat:@"%d",numberOfExpValue ]];
return cell;
}
return nil;
}
和我的AllMonthCell.m文件看起来像这样:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
UIFont *font = [UIFont fontWithName:@"Cochin" size:14.0];
UIFont *fontBold = [UIFont fontWithName:@"Cochin-Bold" size:14.0];
self.frame = CGRectMake(0, 0, 312, 100);
UIView *top = [[UIView alloc]initWithFrame:CGRectMake(4, 0, 304, 1)];
[top setBackgroundColor:[UIColor lightGrayColor]];
UILabel *budgetStringLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 102, 16)];
[budgetStringLabel setTextAlignment:UITextAlignmentLeft];
[budgetStringLabel setFont:font];
[budgetStringLabel setTextColor:[UIColor lightGrayColor]];
[budgetStringLabel setText:@"Month Budget:"];
[budgetStringLabel setBackgroundColor:[self grayColor]];
UILabel *spentStringLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 30, 102, 16)];
[spentStringLabel setTextAlignment:UITextAlignmentLeft];
[spentStringLabel setFont:font];
[spentStringLabel setTextColor:[UIColor lightGrayColor]];
[spentStringLabel setText:@"Spent Money:"];
[spentStringLabel setBackgroundColor:[self grayColor]];
UIView *divide1 = [[UIView alloc]initWithFrame:CGRectMake(10, 50, 292, 1)];
[divide1 setBackgroundColor:[UIColor lightGrayColor]];
UILabel *balanceStringLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 55, 102, 16)];
[balanceStringLabel setTextAlignment:UITextAlignmentLeft];
[balanceStringLabel setFont:font];
[balanceStringLabel setTextColor:[UIColor lightGrayColor]];
[balanceStringLabel setText:@"Month Balance:"];
[balanceStringLabel setBackgroundColor:[self grayColor]];
UIView *divide2 = [[UIView alloc]initWithFrame:CGRectMake(10, 74, 292, 1)];
UIView *divide3 = [[UIView alloc]initWithFrame:CGRectMake(10, 76, 292, 1)];
[divide2 setBackgroundColor:[UIColor lightGrayColor]];
[divide3 setBackgroundColor:[UIColor lightGrayColor]];
UILabel *avgDayStringLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 94, 200, 16)];
[avgDayStringLabel setTextAlignment:UITextAlignmentLeft];
[avgDayStringLabel setFont:font];
[avgDayStringLabel setTextColor:[UIColor lightGrayColor]];
[avgDayStringLabel setText:@"Per Day:"];
[avgDayStringLabel setBackgroundColor:[self grayColor]];
UILabel *numberOfExpStringLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 114, 102, 16)];
[numberOfExpStringLabel setTextAlignment:UITextAlignmentLeft];
[numberOfExpStringLabel setFont:font];
[numberOfExpStringLabel setTextColor:[UIColor lightGrayColor]];
[numberOfExpStringLabel setText:@"Expenses Made:"];
[numberOfExpStringLabel setBackgroundColor:[self grayColor]];
[self addSubview:budgetStringLabel];
[self addSubview:top];
[self addSubview:spentStringLabel];
[self addSubview:divide1];
[self addSubview:balanceStringLabel];
[self addSubview:divide2];
[self addSubview:divide3];
[self addSubview:numberOfExpStringLabel];
[self addSubview:avgDayStringLabel];
self.budgetLabel = [[UILabel alloc]initWithFrame:CGRectMake(200, 10, 102, 16)];
[self.budgetLabel setTextAlignment:UITextAlignmentRight];
[self.budgetLabel setFont:font];
[self.budgetLabel setBackgroundColor:[self grayColor]];
[self.budgetLabel setTextColor:[UIColor darkGrayColor]];
self.spentLabel = [[UILabel alloc]initWithFrame:CGRectMake(200, 30, 102, 16)];
[self.spentLabel setTextAlignment:UITextAlignmentRight];
[self.spentLabel setFont:font];
[self.spentLabel setBackgroundColor:[self grayColor]];
[self.spentLabel setTextColor:[UIColor lightGrayColor]];
self.balanceLabel = [[UILabel alloc]initWithFrame:CGRectMake(200, 55, 102, 16)];
[self.balanceLabel setTextAlignment:UITextAlignmentRight];
[self.balanceLabel setFont:fontBold];
[self.balanceLabel setBackgroundColor:[self grayColor]];
self.avgDayLabel = [[UILabel alloc]initWithFrame:CGRectMake(200, 94, 102, 16)];
[self.avgDayLabel setFont:font];
[self.avgDayLabel setBackgroundColor:[self grayColor]];
[self.avgDayLabel setTextColor:[UIColor lightGrayColor]];
[self.avgDayLabel setTextAlignment:UITextAlignmentRight];
self.numberOfExpLabel = [[UILabel alloc]initWithFrame:CGRectMake(200, 114, 102, 16)];
[self.numberOfExpLabel setTextAlignment:UITextAlignmentRight];
[self.numberOfExpLabel setFont:font];
[self.numberOfExpLabel setBackgroundColor:[self grayColor]];
[self.numberOfExpLabel setTextColor:[UIColor lightGrayColor]];
[self addSubview:self.budgetLabel];
[self addSubview:self.spentLabel];
[self addSubview:self.balanceLabel];
[self addSubview:self.numberOfExpLabel];
[self addSubview:self.avgDayLabel];
[self setBackgroundColor:[self grayColor]];
}
return self;
}
这是滚动时的时间分析的屏幕截图:
所以我创建了AllMonthCell类型的可重用单元格,并为它们赋值,有没有办法让它更快?滚动桌子时我有相当严重的滞后..
暗示会很棒:)
答案 0 :(得分:3)
您应该重复使用单元格
让我为你推导一个模式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"cell";
UILabel *noExpense;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier] autorelease];
//Just the view alone should be created here no assigning of data here
//let me add just one custom view for easy understanding
noExpense = [[UILabel alloc]initWithFrame:CGRectMake(8, 20, 296, 16)];
[noExpense setTextAlignment:UITextAlignmentCenter];
[noExpense setFont:fontName];
//[noExpense setText:@"No data available yet."]; Dont do this
[noExpense setTextColor:[UIColor lightGrayColor]];
[noExpense setBackgroundColor:tableView.backgroundColor];
[noExpense setTag:100]; //Must set tag here
[cell addSubview:noExpense];
}
else
{
noExpense = [cell viewWithTag:100];
//You have to initialize your custom views which you created already.
//Just the view alone should be assigned here no assigning of data here
}
//Load all the data into the views here
[noExpense setText:@"Somedata which you have to set"];
return cell;
}
要了解有关tableview的更多信息,请参阅doc。它是理解表格的圣经。