테이블뷰에 디자인을 적용할 때 팁 몇가지.
1. 테이블뷰 셀 배경색 설정
셀의 배경색 설정은 UITableViewDelegate의 tableView:willDisplayCell:forRowAtIndexPath에서 해야한다.
홀/짝수에 다른 색을 적용하려면 다음과 같이 하면 된다.
1. 테이블뷰 셀 배경색 설정
셀의 배경색 설정은 UITableViewDelegate의 tableView:willDisplayCell:forRowAtIndexPath에서 해야한다.
홀/짝수에 다른 색을 적용하려면 다음과 같이 하면 된다.
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row % 2 == 0)
{
cell.backgroundColor = [UIColor redColor];
}
else
{
cell.backgroundColor = [UIColor blueColor];
}
{
if (indexPath.row % 2 == 0)
{
cell.backgroundColor = [UIColor redColor];
}
else
{
cell.backgroundColor = [UIColor blueColor];
}
}
2. 셀 선택시 배경색 설정
셀을 선택했을 때의 배경색은 UITableViewCell의 selectedBackgroundView를 설정하면 된다.
셀을 생성할 때 설정하면 되는데, storyboard를 이용하면 UITableViewCell의 initWithStyle이 불리지 않는다.
대신 NSCodingProtocol의 initWithCoder가 불리므로 UITableViewCell을 상속받은 클래스에 다음과 같은 함수를 추가하면 된다.
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:[UIColor redColor]];
[self setSelectedBackgroundView:bgColorView];
}
return self;
}
3. 테이블뷰로 되돌아왔을 때 선택 해제하기
View controller의 viewWillAppear에서 선택을 해제한다.
- (void)viewWillAppear:(BOOL)animated
{
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
}