簡體   English   中英

使用自定義單元格在UITableView上具有更好的性能

[英]Better performance on UITableView with Custom cells

我想擺脫UITableView上的滯后,我不知道如何獲得更好的性能。

1-我有6個不同的自定義單元格。 2-細胞膨脹和塌陷。 3-當其中一個可擴展單元格擴展時,在創建列表的下方會生成其他簡單單元格。

處理大量不同種類的單元格時,我的代碼變得非常復雜。 他們每個人都有不同的行為和不同的身高。

我讀過一些關於它的問題,有人說可以改進UITableView的一件事是在傳遞此方法時在Height上創建某種緩存:

*我不知道該如何緩存,我不確定這是否可以解決我的問題。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

我的代碼現在如何:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    BOOL isChild =
    currentExpandedIndex > -1
    && indexPath.row > currentExpandedIndex
    && indexPath.row <= currentExpandedIndex + [[subItems objectAtIndex:currentExpandedIndex] count];

    if (indexPath.row == 0) {
        return 117;
    }

    if (currentExpandedIndex == 0) {

        if (isChild && indexPath.row == currentExpandedIndex +1) {
            return 175;
        }else if (isChild){
            return 0;
        }

        if (indexPath.row == currentExpandedIndex + numberOfExpandedCells + 3) {
            return 15;
        }

    }

    if (currentExpandedIndex == 2) {
        if (indexPath.row == currentExpandedIndex + numberOfExpandedCells + 1) {
            return 15;
        }
    }

    if (!isChild && indexPath.row == 1) {
        return 15;
    }else if (!isChild && indexPath.row == 3){
        return 15;
    }else if (!isChild && indexPath.row == 11 + numberOfExpandedCells){
        return 100;
    }


    return 55;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BOOL isChild =
    currentExpandedIndex > -1
    && indexPath.row > currentExpandedIndex
    && indexPath.row <= currentExpandedIndex + [[subItems objectAtIndex:currentExpandedIndex] count];


    if (!isChild) {

        static NSString *PanelCellIdentifier = @"PanelCell";
        static NSString *ParentCellIdentifier = @"ParentCell";
        static NSString *TutorialCellIdentifier = @"TutorialCell";

        if (indexPath.row == 0) {

            DSPanelTableViewCell *cell = (DSPanelTableViewCell *)[tableView dequeueReusableCellWithIdentifier:PanelCellIdentifier];

            if (cell == nil) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"DSPanelTableViewCell" owner:nil options:nil] objectAtIndex:0];
            }

            [cell configurePanelCellFor:user.recomendacoes with:user.pontos expanded:currentExpandedIndex withCalc:calc];

            return cell;

        }else{


            DSMealTableViewCell *cell = (DSMealTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ParentCellIdentifier];
            if (cell == nil) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"DSMealTableViewCell" owner:nil options:nil] objectAtIndex:0];
            }
            int topIndex = (currentExpandedIndex > -1 && indexPath.row > currentExpandedIndex)
            ? indexPath.row - [[subItems objectAtIndex:currentExpandedIndex] count]
            : indexPath.row;

            [cell configureMealCellWithTitles:titlesArray subTitles:subtitlesArray points:pointsArray minMax:minMax forIndex:topIndex];

            if (currentExpandedIndex == 0) {
                if (indexPath.row == 2 + numberOfExpandedCells){
                    CGRect frame = cell.title.frame;
                    frame.origin.y += 8;
                    cell.title.frame = frame;
                    cell.subTitle.hidden = YES;
                }
            }else if (indexPath.row == 2 || indexPath.row == 9 + numberOfExpandedCells) {

                CGRect frame = cell.title.frame;
                frame.origin.y += 8;
                cell.title.frame = frame;
                cell.subTitle.hidden = YES;

            }else{
               cell.subTitle.hidden = NO;
            }


            if (indexPath.row == 10 + numberOfExpandedCells) {
                DSTutorialTableViewCell *cell = (DSTutorialTableViewCell *)[tableView dequeueReusableCellWithIdentifier:TutorialCellIdentifier];
                if (cell == nil) {
                    cell = [[[NSBundle mainBundle] loadNibNamed:@"DSTutorialTableViewCell" owner:nil options:nil] objectAtIndex:0];
                }
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                cell.delegate = self;
                cell.cellIndex = indexPath.row;
                return cell;
            }


            if ((currentExpandedIndex == 0 && indexPath.row == currentExpandedIndex + numberOfExpandedCells + 3) ||
                (currentExpandedIndex == 2 && indexPath.row == currentExpandedIndex + numberOfExpandedCells + 1) ||
                (indexPath.row == 1 || indexPath.row == 3 || indexPath.row == 11 + numberOfExpandedCells)) {
                cell.userInteractionEnabled = NO;
                cell.hidden = YES;
            }else{
                cell.userInteractionEnabled = YES;
                cell.hidden = NO;
            }

            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }

    }else{

        static NSString *PanelCellIdentifier = @"PanelCell";
        static NSString *ButtonCellIdentifier = @"ButtonCell";
        static NSString *ChildCellIdentifier = @"ChildCell";


        if (indexPath.row == currentExpandedIndex +1 ) {

            if (currentExpandedIndex == 0) {

                DSPanelInfoTableViewCell *cell = (DSPanelInfoTableViewCell *)[tableView dequeueReusableCellWithIdentifier:PanelCellIdentifier];
                if (cell == nil) {
                    cell = [[[NSBundle mainBundle] loadNibNamed:@"DSPanelInfoTableViewCell" owner:nil options:nil] objectAtIndex:0];
                }

                [cell configurePanelInfoCellWithPontos:user.pontos withCalc:calc];

                return cell;
            }

            DSButtonsTableViewCell *cell = (DSButtonsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ButtonCellIdentifier];
            if (cell == nil) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"DSButtonsTableViewCell" owner:nil options:nil] objectAtIndex:0];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.delegate = self;
            cell.cellIndex = indexPath.row;
            return cell;
        }


        DSItemTableViewCell *cell = (DSItemTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChildCellIdentifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"DSItemTableViewCell" owner:nil options:nil] objectAtIndex:0];
        }


        if (currentExpandedIndex == 0) {
            cell.userInteractionEnabled = NO;
            cell.hidden = YES;
            return cell;
        }

        NSDictionary* dict = [[subItems objectAtIndex:currentExpandedIndex] objectAtIndex:indexPath.row - currentExpandedIndex - 1];
        [cell configureItemCellWithDetails:dict];

        return cell;
    }

}

我知道..哇,這太多的代碼無法處理UITableView ...但是我不知道如何改善它。 這段代碼可以正常工作,但是tableview的性能很差。 有人可以幫忙嗎?

你必須設置reuseIdentifier在你的細胞的.xib文件。 否則它們將不會被重用,並且性能會下降。

您的代碼很難閱讀,有點復雜...

您可以簡化很多...嘗試使用函數來分隔單元格...

這是我一直使用的一種優雅方式。.這僅用於創建多個自定義單元格,沒有用於計算單元格高度的任何代碼。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    /*
       Call a function to create all custom cells.
       Send the tableview and the indexPath to this function.
       So, your code will be clean and easy to read an maintenance =D
       DON'T forget to change the height of each cell
       Create all the if you want to call the function for creating their cells
    */
    if (indexPath.row < 3){
        return [self createACustomCell1:tableView indexPath:indexPath];
    }else{
        return [self createACustomCell2:tableView indexPath:indexPath];
    }
}


//*************
//  Create CUSTOM CELL 1
//*************
-(UITableViewCell *)createACustomCell1:(UITableView *)anTableView indexPath:(NSIndexPath *)indexPath{
    static NSString *CUSTOMCELL_1  = @"CUSTOMCELL_1";

    CustomCell_1 *cell = [anTableView dequeueReusableCellWithIdentifier:CUSTOMCELL_1];
    if (!cell){
    [anTableView registerNib:[UINib nibWithNibName:CUSTOMCELL_1
                                          bundle:nil] forCellReuseIdentifier:CUSTOMCELL_1];
        cell = [anTableView dequeueReusableCellWithIdentifier:CUSTOMCELL_1];
    }

    // Cell customization above 
    return cell;
}


//*************
//  Create CUSTOM CELL 2
//*************
-(UITableViewCell *)createACustomCell2:(UITableView *)anTableView indexPath:(NSIndexPath *)indexPath{
    static NSString *CUSTOMCELL_2  = @"CUSTOMCELL_2";

    CustomCell_2 *cell = [anTableView dequeueReusableCellWithIdentifier:CUSTOMCELL_2];
    if (!cell){
    [anTableView registerNib:[UINib nibWithNibName:CUSTOMCELL_2
                                          bundle:nil] forCellReuseIdentifier:CUSTOMCELL_2];
        cell = [anTableView dequeueReusableCellWithIdentifier:CUSTOMCELL_2];
    }

    // Cell customization above


    return cell;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM