在我的应用程序中,我正在使用一个表。现在我想用替代颜色分隔两行。这意味着我的第一行将具有白色,我的第二行将具有灰色,第三行将再次具有白色...所以请任何人都有解决方案。然后请分享。谢谢。
Akshay
答案 0 :(得分:10)
这是一个相对简单的实现:
在tableViewController
中使用以下内容实现cellForRow
:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"DefaultCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// check if row is odd or even and set color accordingly
if (indexPath.row % 2) {
cell.backgroundColor = [UIColor whiteColor];
}else {
cell.backgroundColor = [UIColor lightGrayColor];
}
return cell;
}
答案 1 :(得分:1)
最高投票的答案并不准确。在iOS6上,需要设置contentView的背景颜色:
// check if row is odd or even and set color accordingly
if (indexPath.row % 2) {
cell.contentView.backgroundColor = [UIColor whiteColor];
}else {
cell.contentView.backgroundColor = [UIColor lightGrayColor];
}