在 xcode 中为表格视图中的备用单元格赋予颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6174027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Giving colour to the alternate cells in table view in xcode
提问by Christina
I want to give alternate colors to my table view cell. e.g first cell color remains white while the next cell is light gray ,then third again white ,then next is again light gray and so on.Can anyone tell me the method to do it please.
我想为我的表格视图单元格提供替代颜色。例如第一个单元格颜色保持白色,而下一个单元格是浅灰色,然后第三个又是白色,然后又是浅灰色等等。谁能告诉我这样做的方法。
Thanks, Christy
谢谢,克里斯蒂
回答by Suny
use this christy:
使用这个克里斯蒂:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0 || indexPath.row%2 == 0) {
UIColor *altCellColor = [UIColor colorWithRed:100 green:10 blue:10 alpha:1];
cell.backgroundColor = altCellColor;
}
}
回答by Mikayil Abdullayev
In your cell:(UITableviewCell*) forRowAtIndexPath:(NSIndexPath*) indexPath
Data source method insert this test:
在您的cell:(UITableviewCell*) forRowAtIndexPath:(NSIndexPath*) indexPath
数据源方法中插入此测试:
NSUinteger row=indexPath.row;
if (row % 2==0)
cell.contentView.backGroundColor=[UIColor blueClor] ;
else
cell.contentView.backGroundColor=[UIColor whiteClor] ;
回答by Tariq
Try this Christy in much generic way:
以非常通用的方式尝试这个 Christy:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.backgroundColor = indexPath.row % 2 ? [UIColor lightGrayColor] : [UIColor whiteColor];
}
回答by Karthikeyan
Hii Christina :
嗨,克里斯蒂娜:
Check this out,
看一下这个,
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if ((indexPath.row % 2) == 0) {
cell.backgroundColor = [UIColor whiteColor];
}
else {
cell.backgroundColor = [UIColor lightgrayColor];
}
}
Hope this Helps!
希望这可以帮助!
回答by Jhaliya
Use the below code when you create the UITableViewCell
.
创建UITableViewCell
.
if(indexPath.row % 2 == 0)
{
myCell.backgroundColor = [UIColor whiteColor];
}
else if(indexPath.row % 2 == 1)
{
myCell.backgroundColor = [UIColor grayColor];
}