简体   繁体   English

在iPhone上设置表格视图单元格的背景颜色

[英]Setting background color of a table view cell on iPhone

I want to achieve the effect where one cell of the table view will have blue background, the next one will have white, the next one will have blue again, and then white and so on... could you let me know how can I do that? 我想达到这样的效果,其中表视图的一个单元格将具有蓝色背景,下一个将具有白色,下一个将再次具有蓝色,然后是白色等等...您能告诉我我该怎么办?去做?

Thanks. 谢谢。

Add this method to your table view delegate: 将此方法添加到表视图委托:

#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];
}

You have to set the background color of the cell's content view 您必须设置单元格内容视图的背景颜色

cell.contentView.backgroundColor = [UIColor colorWithRed...]

This will set the background of the whole cell. 这将设置整个单元格的背景。

To do this for alternate cells, use the indexPath.row and % by 2. 要对备用单元格执行此操作,请使用indexPath.row% by 2。

If you want to set cell color based on some state in the actual cell data object, then this is another approach: 如果要根据实际单元格数据对象中的某些状态设置单元格颜色,则这是另一种方法:

If you add this method to your table view delegate: 如果将此方法添加到表视图委托:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = cell.contentView.backgroundColor;
}

Then in your cellForRowAtIndexPath method you can do: 然后在您的cellForRowAtIndexPath方法中,您可以执行以下操作:

if (myCellDataObject.hasSomeStateThatMeansItShouldShowAsBlue) {
    cell.contentView.backgroundColor = [UIColor blueColor];
}

This saves having to retrieve your data objects again in the willDisplayCell method. 这样可以节省在willDisplayCell方法中再次检索数据对象的麻烦。

please add the following code in the cellForRowAtIndexPath 请在cellForRowAtIndexPath中添加以下代码

if (indexPath.row % 2 == 0){
    cell.backgroundColor =[UIColor blueColor];
} else {
    cell.backgroundColor =[UIColor whiteColor];
}

i think this will help you 我想这会对你有所帮助

The cell.contentView.backgroundColor = [UIColor colorWithRed...] method in lostInTransit's answer works, as long as you do not use the built-in label of a UITableViewCell. 只要您不使用UITableViewCell的内置标签,lostInTransit的答案中的cell.contentView.backgroundColor = [UIColor colorWithRed...]方法就可以正常工作。

I found that if you use the built-in label, eg by setting cell.text , you end up with a opaque white block under the label and only the two ends of the cell show your desired color. 我发现如果您使用内置标签,例如通过设置cell.text ,您最终会在标签cell.text出现一个不透明的白色块,并且只有单元格的两端显示您想要的颜色。

I found no way to edit the built-in label so it is non-opaque (you can access it via UILabel* cellLabel = [cell.contentView.subviews objectAtIndex:0] ). 我发现无法编辑内置标签,因此它是非透明的(您可以通过UILabel* cellLabel = [cell.contentView.subviews objectAtIndex:0]访问它)。

I solved the problem by adding my own custom UILabel . 我通过添加自己的自定义UILabel解决了这个问题。 Like this: 像这样:

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];

//time saving method: //节省时间的方法:

//in cell for index method: //在索引方法的单元格中:

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);
}

// change the cell content and then return it. //更改单元格内容然后返回它。 Easy job. 轻松工作。

// this is a outline code. //这是大纲代码。 Please not copy it directly. 请不要直接复制。

This worked for me .. In cellForRowAtIndexPath , 这对我有用..在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;

For you requirement, as mentioned earlier based on indexPath % 2 value you can perform the above function. 对于您的要求,如前面提到的基于indexPath%2的值,您可以执行上述功能。

When using the default table cell setting the following two properties will color both the cell's background and the background color of it's label, avoiding the need to create a custom label as a subview of the cell's contentView. 使用默认表格单元格设置时,以下两个属性将为单元格的背景和标签的背景颜色着色,从而无需创建自定义标签作为单元格contentView的子视图。

cell.textLabel.backgroundColor = [UIColor redColor];
cell.contentView.backgroundColor = [UIColor redColor];

This code is a slightly more clean solution for the case where you are using the built-in text label and don't want the white background color of the text label obscuring the background color. 对于使用内置文本标签并且不希望文本标签的白色背景颜色模糊背景颜色的情况,此代码是一种稍微更干净的解决方案。 This also fixes the issue of violating the rounded corners of a grouped style of table view (which happens when you use cell.contentView.backgroundColor instead of cell.backgroundColor) : 这也解决了违反表视图分组样式的圆角的问题(当您使用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];

All you need to do is add the following code in your table's view controller implementation file. 您需要做的就是在表的视图控制器实现文件中添加以下代码。

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row%2 == 0) {
    UIColor *altCellColor = [UIColor blackColor];
    cell.backgroundColor = altCellColor;
}}

It's then as simple as adding and changing the modulus values. 然后就像添加和更改模数值一样简单。

Add backgroundView to the cell and set its background color of your choice. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM