2014-11-28 2 views
0

내 UITableView에서 래그를 없애고 싶다면 내 성능이 어떻게 향상되는지 모르겠다.사용자 정의 셀을 사용하는 UITableView의 성능 향상

1 - 6 개의 맞춤 셀이 있습니다. 2 - 셀이 확장되거나 축소됩니다. 3 - 확장 가능한 셀 중 하나가 확장되면 다른 간단한 셀이 목록을 만드는 아래에 생성됩니다.

다른 종류의 셀을 처리 할 때 코드가 복잡해졌습니다. 그들 각각은 다른 행동과 다른 높이를 가지고 있습니다.

나는 그것에 대해 다른 사람들의 질문을 읽고, 누군가가이 방법에 전달할 때 jQuery과 개선 할 수있는 것들 중 하나는 높이에 캐시의 어떤 종류를 만드는 것입니다 말했다

* I 돈 캐시하는 법을 알지 못해서 이것이 내 문제에 도움이 될지 모르겠습니다. 지금 얼마나

-(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; 
    } 

} 

알아요 .. 와우 이것은 jQuery과를 처리하기 위해 너무 많은 코드입니다 ...하지만 난 그것을 개선하는 방법을 모르겠어요. 이 코드는 제대로 작동하지만 tableview의 성능이 좋지 않습니다. 누구든지 도와 줄 수 있습니까?

+1

교육용 추측 : .xib에있는 셀에 대해 'reuseIdentifier'를 설정하지 않았습니까? 그렇지 않은 경우 프로파일 러 (Instruments)를 사용하여 성능 저하의 원인이되는 코드 부분을 판별하십시오. –

+0

그것은 작동했습니다 ... 나는 xib에 식별자를 설정하지 않았습니다 ... 덕분에 많이 !! XD – rihurla

+1

그 점을 잘 알고 있으면 대답에 대한 내 의견이 바뀌었다. –

답변

2

세포의 .xib 파일에 reuseIdentifier을 설정해야합니다. 그렇지 않으면 다시 사용되지 않으며 성능이 떨어집니다.

+0

고마워 !!! XD – rihurla

-1

코드는

이에 우아한 방법입니다 ...

당신이 기능을 사용하여 세포를 분리하려고 ... 그것을 많이 단순화 할 수 있습니다 ... 읽고 조금 복잡하기 어렵다 내가 항상 사용하는 것을해라 .. 멀티 셀 사용자 정의 셀을 만드는 경우에만 작동하며 셀 높이를 계산할 수있는 코드는 없다.

-(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; 
} 
+1

이미 대답 해 왔지만 잊어 버린 세부 사항 이었지만 어쨌든 고맙습니다. 나는 코드가 복잡하다고 (복잡한 일을한다) 말할 때 동의하지만, 전혀 읽는 것이 어렵지 않다. 각 셀을 다른 함수로 생성하는 메소드를 만든다. 더 나은 성능. – rihurla