2012-06-21 2 views
0

나는 항상이처럼 cellForRowAtIndexPath 호출 안에 내있는 UITableViewCell의 배경을 설정되었습니다UITableViewCell의 배경보기 설정 - 어떤 방법이 더 좋습니까?

이제
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:simple]; 

if (cell == nil) 
{ 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 

    for (id findCell in nib) 
    { 
     if ([findCell isKindOfClass: [CustomCell class]]) 
     { 
      cell = findCell; 
     }  
    } 

    UIView *cellBackView = [[UIView alloc] initWithFrame:CGRectZero]; 
    UIView *cellSelectedBackView = [[UIView alloc] initWithFrame:CGRectZero]; 

    if (UIUserInterfaceIdiomPad == UI_USER_INTERFACE_IDIOM()) { 

     cellBackView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"cell_shadows_ipad_light.png"]]; 
     cellSelectedBackView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"blue_cell.png"]]; 
    }else { 
     cellBackView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"cell_shadows.png"]]; 
     cellSelectedBackView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"blue_cell.png"]]; 
    } 

    cell.backgroundView = cellBackView; 
    cell.selectedBackgroundView = cellSelectedBackView; 

나는이 위임 내부의 배경 뷰를 설정하는 것입니다 이것을 달성하는 또 다른 방법이 있다는 발견했습니다 :

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{ 
    // load the background image and set it as the background color as above 
} 

그리고 iOS5 이후로 nib를 tableView에 등록 할 수 있으므로 클래스 펜촉을 찾을 때까지 순환 할 필요가 없습니다. 그래서,있는 viewDidLoad에서 :

[self.tableView registerNib:[UINib nibWithNibName: @"CustomCellSplit" bundle:nil] forCellReuseIdentifier: @"CustomCellId"]; 

그래서이 작품, 그것은 간단하고, 셀의 펜촉을로드하고 배경보기를 설정하는 애플에서 권장하는 방법입니다. 그러나, 내가 발견 한 것은이 tableView : willDisplayCell : forRowAtIndexPath가 목록을 스크롤 할 때 각 행에 대해 호출된다는 것입니다. 이전에 백그라운드 뷰를로드 한 방식으로 셀을 만들 때 (최대 8 ~ 10 회)에만 backgroundView를 설정했습니다.

그래서 새로운 방법은 셀을로드하고 backgroundViews를 설정하는 성능이 떨어지는 것처럼 들립니다. 이것은 올바른 가정입니까, 아니면 여기에 뭔가 빠졌습니까?

답변

1

정말 간단합니다. 펜촉에 하위 뷰를 추가하고 UITableViewCell의 콘센트 selectedBackgroundView에 연결하면됩니다.

http://cl.ly/image/322L2k3j2U1Aconnecting selectedBackgroundView outlet from nib

짜잔.

iOS5는 런타임에 셀의 contentView에서 backgroundView를 제거 할만큼 똑똑합니다.

tableView:willDisplayCell 기능을 사용할 필요가 없습니다.

관련 문제