我想达到这样的效果,其中表视图的一个单元格将具有蓝色背景,下一个将具有白色,下一个将再次具有蓝色,然后是白色等等...您能告诉我我该怎么办?去做?
谢谢.
jes*_*rry 73
将此方法添加到表视图委托:
#pragma mark UITableViewDelegate
- (void)tableView: (UITableView*)tableView
willDisplayCell: (UITableViewCell*)cell
forRowAtIndexPath: (NSIndexPath*)indexPath
{
cell.backgroundColor = indexPath.row % 2
? [UIColor colorWithRed: 0.0 green: 0.0 blue: 1.0 alpha: 1.0]
: [UIColor whiteColor];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
}
Run Code Online (Sandbox Code Playgroud)
los*_*sit 43
您必须设置单元格内容视图的背景颜色
cell.contentView.backgroundColor = [UIColor colorWithRed...]
Run Code Online (Sandbox Code Playgroud)
这将设置整个单元格的背景.
要为备用单元格执行此操作,请使用indexPath.row和%by 2.
如果要根据实际单元格数据对象中的某些状态设置单元格颜色,则这是另一种方法:
如果将此方法添加到表视图委托:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = cell.contentView.backgroundColor;
}
Run Code Online (Sandbox Code Playgroud)
然后在您的cellForRowAtIndexPath方法中,您可以执行以下操作:
if (myCellDataObject.hasSomeStateThatMeansItShouldShowAsBlue) {
cell.contentView.backgroundColor = [UIColor blueColor];
}
Run Code Online (Sandbox Code Playgroud)
这样可以节省在willDisplayCell方法中再次检索数据对象的麻烦.
小智 6
请在cellForRowAtIndexPath中添加以下代码
if (indexPath.row % 2 == 0){
cell.backgroundColor =[UIColor blueColor];
} else {
cell.backgroundColor =[UIColor whiteColor];
}
Run Code Online (Sandbox Code Playgroud)
我认为这对您有帮助
| 归档时间: |
|
| 查看次数: |
73870 次 |
| 最近记录: |