2010-06-22 6 views
0

uitableviewcells에 서브 뷰를 추가했는데 위아래로 스크롤하면 하위 뷰가 여러 번 추가됩니다. 내가 틀린 곳에 나는 안식한다. 당신은 그들이 방금 생성 된 또는 재사용되고 있는지 여부를 셀에 하위 뷰를 추가uitableviewcell의 하위 뷰와 관련된 문제

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

static NSString *CellIdentifier = @"MainPageCellView"; 
MainPageTableCellView *cell = (MainPageTableCellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) { 
    [[NSBundle mainBundle] loadNibNamed:@"MainPageTableCellView" owner:self options:nil]; 
    cell = mainPageTableCell; 
} 



[cell setNameText:[namesArray objectAtIndex:indexPath.row]]; 
[cell setPositionText:[positionArray objectAtIndex:indexPath.row]]; 
[cell setCompanyNameText:[companyNameArray objectAtIndex:indexPath.row]]; 
[cell setDistanceText:[distArray objectAtIndex:indexPath.row]]; 
[cell setImageViewImage:[imagesArray objectAtIndex:indexPath.row]]; 


UIImage *halo = [UIImage imageNamed:@"h1.png"]; 
UIImageView *haloV = [[UIImageView alloc] initWithImage:halo]; 

if(indexPath.row == 0) 
[cell addSubview: haloV]; 
else if(indexPath.row ==2) 
[cell addSubview: haloV]; 
else if(indexPath.row ==3) 
[cell addSubview: haloV]; 


return cell; 

} 

답변

1

:

여기 내 코드입니다. 위아래로 스크롤하면 다시 사용되어 하위 뷰가 반복적으로 추가됩니다.

if (cell == nil) 블록 내에서 addSubview 호출을 이동하면 문제가 해결됩니다.

if (cell == nil) { 
    [[NSBundle mainBundle] loadNibNamed:@"MainPageTableCellView" owner:self options:nil]; 
    cell = mainPageTableCell; 

    UIImage *halo = [UIImage imageNamed:@"h1.png"]; 
    UIImageView *haloV = [[UIImageView alloc] initWithImage:halo]; 

    if(indexPath.row == 0) 
    [cell addSubview: haloV]; 
    else if(indexPath.row ==2) 
    [cell addSubview: haloV]; 
    else if(indexPath.row ==3) 
    [cell addSubview: haloV]; 
} 
+0

감사합니다 ... 괜찮습니다. ..... – mano

+0

좋습니다. 그것이 맞다면 대답과 upvote를 수락하십시오. –

관련 문제