사용자 정의 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:
으로 설정해야합니까? 이 상황을 어떻게 처리해야합니까?