我需要为uitableview性能ipad做更多优化

时间:2014-06-03 09:01:51

标签: ios performance ipad uitableview

我在堆栈溢出时经历了不同的线程,并在我的表视图实现中采用了它们。 其中一些是:

tblOrderView.delaysContentTouches = NO;//order performance
tblOrderView.canCancelContentTouches = NO;//order performance
tblOrderView.showsHorizontalScrollIndicator = NO;//order performance

我正在使用不同的重新加载变体,例如在索引处重新加载行并在索引路径处插入行,这是可能的,而不是重新加载数据。

我通过c == nil检查以适当的方式重复使用单元格,在单元格中创建内容视图== nil并通过带标记的视图访问内容视图。

我不是一次又一次地获取UIImage图像名称,我在实例变量中保留了一个引用。

根据客户提供的设计,我的表格视图,单元格和内容必须是透明的。

我不确定我的heightForRowAtIndexPath实现请看下面的行格式....实现,它按预期工作。 问题:当我在30到35次插入后继续在索引路径插入行时,我的表性能下降(我调用插入行而不是重新加载数据以获得高性能)。 行相关代码的单元格是:

NSString *cellIdentifier=[NSString stringWithFormat:@"Modifiers%d",numberOfModifiers];;

customCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
[customCell setSelected:NO animated:NO];
if(customCell==nil){
    [customCell setBackgroundColor:[UIColor clearColor]];
    customCell=[self reusableTableViewCellContent:cellIdentifier ForModifier:numberOfModifiers indexPath:indexPath];
}

reusableTableViewCellContent:cellIdentifier实施是:

-(UITableViewCell *)reusableTableViewCellContent:(NSString *)identifier ForModifier:(int)_modifier indexPath:(NSIndexPath *)indexPath{

UITableViewCell *cell=nil;


//NSLog(@"%@",identifier);
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
cell.selectionStyle=UITableViewCellSelectionStyleNone;


CustomButton *btnDecimalQty=[[CustomButton alloc]initWithFrame:CGRectMake(5, 10, 57, 24)];

[btnDecimalQty setBackgroundImage:imgQuantity forState:UIControlStateNormal];
[btnDecimalQty setTag:DECIMAL_QUANTITY_BUTTON];
btnDecimalQty.titleLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:15.0];
[btnDecimalQty setTitleColor:[UIColor colorWithRed:71.0/255.0 green:72.0/255.0 blue:72.0/255.0 alpha:1.0] forState:UIControlStateNormal];
[btnDecimalQty addTarget:self action:@selector(openKeyPadPopUp:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:btnDecimalQty];

float prod_origin_x = btnDecimalQty.frame.origin.x + btnDecimalQty.frame.size.width + 5;



UILabel *productName=[[UILabel alloc]initWithFrame:CGRectMake(prod_origin_x , 8, 178, 30)]; //163, 140, 220, 30
//  [productName setText:@"Potato Wedges"];
[productName setFont:[UIFont fontWithName:@"Arial-BoldMT" size:[[Utils getFontSize] floatValue]]];
[productName setBackgroundColor:[UIColor clearColor]];
[productName setTextAlignment:UITextAlignmentLeft];
[productName setTextColor:[UIColor colorWithRed:45.0/255.0 green:44.0/255.0 blue:44.0/255.0 alpha:1.0]];
[productName setTag:PRODUCT_NAME_LABEL];
[cell.contentView addSubview:productName];


float action_button_origin_x = productName.frame.origin.x + productName.frame.size.width + 2;



CustomButton *edit=[[CustomButton alloc]initWithFrame:CGRectMake(action_button_origin_x, 14, 31, 23)];
[edit setTag:EDIT_BUTTON];


[edit setBackgroundImage:imgEdit forState:UIControlStateNormal];
[edit addTarget:self action:@selector(edit:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:edit];



CustomButton *discount=[[CustomButton alloc]initWithFrame:CGRectMake(edit.frame.origin.x+0 + edit.frame.size.width + 2, 14, 31, 23)];
[discount setTag:DISCOUNT_BUTTON];

[discount setBackgroundImage:imgDiscount forState:UIControlStateNormal];
[discount addTarget:self action:@selector(discount:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:discount];

float prod_amount_orgin_x = discount.frame.origin.x + discount.frame.size.width + 1;


UILabel *amount=[[UILabel alloc]initWithFrame:CGRectMake(prod_amount_orgin_x , 8, 65, 30)];
//  [amount setText:@"$ 70.00"];
[amount setBackgroundColor:[UIColor clearColor]];
[amount setTextAlignment:UITextAlignmentRight];
[amount setAdjustsFontSizeToFitWidth:YES];
[amount setFont:[UIFont fontWithName:@"Arial-BoldMT" size:[[Utils getFontSize] floatValue]]];
[amount setTextColor:[UIColor colorWithRed:71.0/255.0 green:72.0/255.0 blue:72.0/255.0 alpha:1.0]];
[amount setTag:AMOUNT_LABEL];
[cell.contentView addSubview:amount];


for(int i=0;i<_modifier ;i++)
{
    UILabel *modifierName=[[UILabel alloc]initWithFrame:CGRectMake(prod_origin_x, 30+(i*30), 220, 30)];
    //      [modifierName setText:@"Modifier 1"];
    [modifierName setFont:[UIFont fontWithName:@"Arial-BoldMT" size:[[Utils getFontSize] floatValue]]];
    [modifierName setBackgroundColor:[UIColor clearColor]];
    [modifierName setTextAlignment:UITextAlignmentLeft];
    [modifierName setTextColor:[UIColor colorWithRed:45.0/255.0 green:44.0/255.0 blue:44.0/255.0 alpha:1.0]];
    [modifierName setTag:MODIFIER_NAME_LABEL+(i*2)];
    [cell.contentView addSubview:modifierName];


    UILabel *modifierAmount=[[UILabel alloc]initWithFrame:CGRectMake(prod_amount_orgin_x, 30+(i*30), 65, 30)];
    //      [modifierAmount setText:@"$0.50"];
    [modifierAmount setBackgroundColor:[UIColor clearColor]];
    [modifierAmount setTextAlignment:UITextAlignmentRight];
    [modifierAmount setFont:[UIFont fontWithName:@"Arial-BoldMT" size:[[Utils getFontSize] floatValue]]];
    [modifierAmount setTextColor:[UIColor colorWithRed:71.0/255.0 green:72.0/255.0 blue:72.0/255.0 alpha:1.0]];
    [modifierAmount setTag:MODIFIER_AMOUNT_LABEL+(i*2)];
    [modifierAmount setAdjustsFontSizeToFitWidth:YES];
    [cell.contentView addSubview:modifierAmount];

}


    return cell;

}

我的heightForRow实现....

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Product *product=(Product *)[self.arrOrderDetail objectAtIndex:indexPath.row];

    //Modifiers
    NSArray *modifierList=product.mModifiersAdded;
    int numberOfModifiers=0;
    for(Modifier *modifier in modifierList)
    {
        for(ModifierItem *modifierItem in modifier.activeModifiers)
        {
            numberOfModifiers++;
        }
    }

    OrderModule *module=[OrderModule sharedModule];
    numberOfModifiers=numberOfModifiers+[[module getActiveCustomModifiers:product.mOrderDetailId] count];


    //Non time based discount
    if([product.mNonTimeBasedDiscounts floatValue]>0.0)
    {
        numberOfModifiers++;
    }
    else if([product.mRuntimeDiscount floatValue]>0.0)       //Run time based discount
    {
        numberOfModifiers++;
    }
    else if([product.mTimeBasedDiscounts floatValue]>0.0)
    {
        numberOfModifiers++;
    }


    //Amount Modifier
    if(numberOfModifiers>0)
    {
        numberOfModifiers++;
    }

    CGFloat extraHeight=0.0;
    if(numberOfModifiers==0)
        extraHeight=15.0;
    return 30.0+extraHeight+(numberOfModifiers*30.0);
}

请建议我可以做些什么来提高uitableview性能(重新加载和滚动)

感谢。

1 个答案:

答案 0 :(得分:1)

我假设您正在设置UILabel标记,以便在提供内容时可以搜索字段?为什么不创建一个将标签存储在两个NSMutableArrays中的自定义UITableViewCell子类?那么你可以从数组中选择合适的标签,而不是必须通过视图层次结构对每个单元格中的每个标签进行线性搜索?

设置单元格时,可以在循环中调用几个可以在外部调用的方法,这样就可以了

UIFont *arialFont=[UIFont fontWithName:@"Arial-BoldMT" size:[[Utils getFontSize] floatValue]];
UIColor *amountColor=[UIColor colorWithRed:71.0/255.0 green:72.0/255.0 blue:72.0/255.0 alpha:1.0];
UIColor *nameColor=[UIColor colorWithRed:45.0/255.0 green:44.0/255.0 blue:44.0/255.0 alpha:1.0];

UILabel *amount=[[UILabel alloc]initWithFrame:CGRectMake(prod_amount_orgin_x , 8, 65, 30)];
//  [amount setText:@"$ 70.00"];
[amount setBackgroundColor:[UIColor clearColor]];
[amount setTextAlignment:UITextAlignmentRight];
[amount setAdjustsFontSizeToFitWidth:YES];
[amount setFont:arialFont];
[amount setTextColor:amountColor];
[amount setTag:AMOUNT_LABEL];
[cell.contentView addSubview:amount];


for(int i=0;i<_modifier ;i++)
{
    UILabel *modifierName=[[UILabel alloc]initWithFrame:CGRectMake(prod_origin_x, 30+(i*30), 220, 30)];
    //      [modifierName setText:@"Modifier 1"];
    [modifierName setFont: setFont:arialFont];
    [modifierName setBackgroundColor:[UIColor clearColor]];
    [modifierName setTextAlignment:UITextAlignmentLeft];
    [modifierName setTextColor: nameColor];
    [modifierName setTag:MODIFIER_NAME_LABEL+(i*2)];
    [cell.contentView addSubview:modifierName];


    UILabel *modifierAmount=[[UILabel alloc]initWithFrame:CGRectMake(prod_amount_orgin_x, 30+(i*30), 65, 30)];
    //      [modifierAmount setText:@"$0.50"];
    [modifierAmount setBackgroundColor:[UIColor clearColor]];
    [modifierAmount setTextAlignment:UITextAlignmentRight];
    [modifierAmount setFont: setFont:arialFont];
    [modifierAmount setTextColor:amountColor];
    [modifierAmount setTag:MODIFIER_AMOUNT_LABEL+(i*2)];
    [modifierAmount setAdjustsFontSizeToFitWidth:YES];
    [cell.contentView addSubview:modifierAmount];

}

此外,在此代码中 -

for(Modifier *modifier in modifierList)
{
    for(ModifierItem *modifierItem in modifier.activeModifiers)
    {
        numberOfModifiers++;
    }
}

modifier.activeModifiers是什么类型的?如果是NSArrayNSDictionary,那么内部循环可以简单地替换为numberOfModifiers+=[modifier.activeModifiers count];