2010-04-09 4 views
12

UITableViewCells의 backgroundColor 레이블을 설정하려고하는데 전혀 아무것도 수행하지 않습니다. 이 일을하는 또 다른 방법이 있는지 궁금합니다. 그래서 물어 봅니다!UITableViewCell의 textLabel.backgroundColor가 작동하지 않습니다.

나는이 시도 : 무엇을

cell.textLabel.backgroundColor = [UIColor redColor]; 
cell.backgroundColor = [UIColor redColor]; 

을 그리고 그것은 작동하지 않는 이유는 무엇입니까?

감사합니다.

답변

41

저는 이것을 cellForRowAtIndexPath에서 사용한다고 가정합니다.

UITableViewCell class reference에 따르면

참고 : ( 있는 UIView에 의해 선언 된 의 backgroundColor 속성을 통해 셀의 배경 색상 를 설정하여) 셀의 배경색을 변경하려는 경우를 해야합니다. tableView : willDisplayCell : forRowAtIndexPath : 위임자가 아닌 tableView : cellForRowAtIndexPath : 데이터 원본.

그래서 이렇게 :

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    cell.backgroundColor = [UIColor redColor]; //must do here in willDisplayCell 
    cell.textLabel.backgroundColor = [UIColor redColor]; //must do here in willDisplayCell 
    cell.textLabel.textColor = [UIColor yellowColor]; //can do here OR in cellForRowAtIndexPath 
} 

당신이 셀 디스플레이를 통해 세밀한 제어가 필요한 경우, 쉬운 방법은 IB 그것을 디자인하고 cellForRowAtIndexPath에서 펜촉에서로드하는 것입니다. 테이블 뷰 프로그래밍 가이드의 this page에있는 "Nib 파일에서 사용자 정의 테이블 뷰 셀로드하기"섹션을 참조하십시오.

+1

신난다. 감사 –

0

IOS 9.3 업데이트. 이것은 실제로 IOS 9.3을 사용하는 cellForRowAtIndexPath에서 잘 작동합니다. 참고 : 전체 셀의 색상을 변경하려면 다음을 사용하십시오.

cell.contentView.backgroundColor = [UIColor redColor];

관련 문제