[英]how to set the background color for single cell in the table view on iphone
[英]Setting background color of a table view cell on iPhone
我想达到这样的效果,其中表视图的一个单元格将具有蓝色背景,下一个将具有白色,下一个将再次具有蓝色,然后是白色等等...您能告诉我我该怎么办?去做?
谢谢。
将此方法添加到表视图委托:
#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];
}
您必须设置单元格内容视图的背景颜色
cell.contentView.backgroundColor = [UIColor colorWithRed...]
这将设置整个单元格的背景。
要对备用单元格执行此操作,请使用indexPath.row
和%
by 2。
如果要根据实际单元格数据对象中的某些状态设置单元格颜色,则这是另一种方法:
如果将此方法添加到表视图委托:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = cell.contentView.backgroundColor;
}
然后在您的cellForRowAtIndexPath方法中,您可以执行以下操作:
if (myCellDataObject.hasSomeStateThatMeansItShouldShowAsBlue) {
cell.contentView.backgroundColor = [UIColor blueColor];
}
这样可以节省在willDisplayCell方法中再次检索数据对象的麻烦。
请在cellForRowAtIndexPath中添加以下代码
if (indexPath.row % 2 == 0){
cell.backgroundColor =[UIColor blueColor];
} else {
cell.backgroundColor =[UIColor whiteColor];
}
我想这会对你有所帮助
只要您不使用UITableViewCell的内置标签,lostInTransit的答案中的cell.contentView.backgroundColor = [UIColor colorWithRed...]
方法就可以正常工作。
我发现如果您使用内置标签,例如通过设置cell.text
,您最终会在标签cell.text
出现一个不透明的白色块,并且只有单元格的两端显示您想要的颜色。
我发现无法编辑内置标签,因此它是非透明的(您可以通过UILabel* cellLabel = [cell.contentView.subviews objectAtIndex:0]
访问它)。
我通过添加自己的自定义UILabel
解决了这个问题。 像这样:
UILabel* cellLabel = [[[UILabel alloc] initWithFrame:cell.frame] autorelease];
cellLabel.text = @"Test with non-opaque label";
cellLabel.backgroundColor = [UIColor clearColor];
cellLabel.opaque = NO;
[cell.contentView addSubview:cellLabel];
cell.contentView.backgroundColor = [UIColor darkGrayColor];
//节省时间的方法:
//在索引方法的单元格中:
static NSString* evenIdentifier = @"even";
static NSString* oddIdentifier = @"odd";
__weak identifier;
if(indexPath.row %2 ==0)
{
identifier = evenIdentifier;
}else{
identifier = oddIdentifier;
}
cell = dequeue..WithIdentifier: identifier;
if(cell == nil){
cell = allocOrLoadNib...;
cell.backgroundColor = (indexPath.row %2 ==0 ? evenColor : oddColor);
}
//更改单元格内容然后返回它。 轻松工作。
//这是大纲代码。 请不要直接复制。
这对我有用..在cellForRowAtIndexPath中,
cell.textLabel.backgroundColor = [UIColor clear];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.contentView.backgroundColor = [UIColor grayColor]; //whichever color u want
cell.backgroundColor = cell.contentView.backgroundColor;
对于您的要求,如前面提到的基于indexPath%2的值,您可以执行上述功能。
使用默认表格单元格设置时,以下两个属性将为单元格的背景和标签的背景颜色着色,从而无需创建自定义标签作为单元格contentView的子视图。
cell.textLabel.backgroundColor = [UIColor redColor];
cell.contentView.backgroundColor = [UIColor redColor];
对于使用内置文本标签并且不希望文本标签的白色背景颜色模糊背景颜色的情况,此代码是一种稍微更干净的解决方案。 这也解决了违反表视图分组样式的圆角的问题(当您使用cell.contentView.backgroundColor而不是cell.backgroundColor时会发生这种情况):
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.backgroundColor = [UIColor redColor];
cell.textLabel.backgroundColor = [UIColor clearColor]; //transparent background
UIView *bg = [[UIView alloc] initWithFrame:cell.frame];
bg.backgroundColor = [UIColor colorWithRed:175.0/255.0 green:220.0/255.0 blue:186.0/255.0 alpha:1];
cell.backgroundView = bg;
[bg release];
您需要做的就是在表的视图控制器实现文件中添加以下代码。
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row%2 == 0) {
UIColor *altCellColor = [UIColor blackColor];
cell.backgroundColor = altCellColor;
}}
然后就像添加和更改模数值一样简单。
将backgroundView
添加到单元格并设置其选择的背景颜色。
let backgroundView = View() // add background view via code or via storyboard
view.backgroundColor = UIColor.red // your color
cell.backgroundView = backgroundView
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.