2011-01-20 3 views
1

사용자 정의 backgroundViews가있는 셀을 사용하는 UITableView가 있습니다. 배경색을 tableView:cellForRowAtIndexPath:indexPath:에 지정하고 here을 지정합니다. 요컨대, UITableView 내 UITableViewCell의 위치에 따라 특정 backgroundView 이미지를 할당하려고합니다. 문제의 코드는 다음과 같습니다.UITableViewCell의 backgroundView 속성 캐싱

UIImage *rowBackground = nil; 

if (row == 0 && row == sectionRows - 1) { 
    rowBackground = [UIImage imageNamed:@"row_bg_start_and_end.png"]; 
} else if (row == 0) { 
    rowBackground = [UIImage imageNamed:@"row_bg_start.png"]; 
} else if (row == sectionRows - 1) { 
    rowBackground = [UIImage imageNamed:@"row_bg_end.png"]; 
} else { 
    rowBackground = [UIImage imageNamed:@"row_bg.png"]; 
} 

((UIImageView *)cell.backgroundView).image = rowBackground; 

이 코드는 잘 작동하며 예상 한 결과가 나타납니다. 그러나 행의 제거 또는 추가에 관해서는 문제가 있습니다. 행의 추가 또는 제거는 특정 행이 배치를 다시 계산하고 문제의 뷰를 업데이트하는 대신 이전 backgroundView를 유지한다는 것을 의미합니다.

dequeueReusableCellWithIdentifier의 목적과 을 이해하는 이유는입니다. 하지만 정확히 어떻게 을 고쳐야할지 모르겠습니다.. 화면 밖으로 스크롤하여 tableView:cellForRowAtIndexPath:indexPath:을 호출하면 행이 올바르게 다시 계산된다는 것을 알 수 있습니다. 결과적으로 backgroundView가 올바르게 재설정됩니다.

backgroundView 속성을 willDisplayCell:forRowAtIndexPath:으로 설정해야합니까? 이 상황을 어떻게 처리해야합니까?

답변

0

문제를 해결하는 가장 쉬운 방법은 섹션에 새 행을 추가 할 때 보이는 모든 행의 배경 이미지를 다시 처리하는 것입니다.

for (UITableViewCell *cell in aTableView.visibleCells) 
{ 
    NSIndexPath *cellIndexPath = [aTableView indexPathForCell:cell]; 

    /* look at the cell.backgroundImage and if it's not appropriate 
    for the indexPath, update it */ 
} 
당신은 신중하게 새로운 행을 삽입 만 업데이트 할 필요가 특정 행에 대해이 작업을 수행하는 확인 모든 jQuery과 메소드를 오버라이드 (override) 할 수 있지만이이 가치가있을 것입니다 생각하지 않습니다

노력.

관련 문제