开发者

Objective C: How to add color to entire cell (with a disclosure indicator)

I am trying to add background color to my cell but was not able to color the portion with the disclosure indicator (see screenshot below)

Objective C: How to add color to entire cell (with a disclosure indicator)

What can I do to color the entire cell? My implementation as follows.

cell.contentView.backgroundColor = [UIColor colorWithHexSt开发者_如何学Goring:@"#FFFFCC"];

//Clear colors for labels
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];


UITableViewCell subclasses UIView; you can set its background color just as you can any other view’s. Cells in a “grouped”-style table view require some more work there, but it doesn’t look like you’re using that, so:

cell.backgroundColor = [UIColor colorWithHexString:@"#FFFFCC"];


My code uses (in the cell itself) self.backgroundColor, so you should uses cell.backgroundColor instead of cell.contentView.backgroundColor. The contentView does not include the selection indicator view, which you have guessed I suppose ;)

If you only want it to change colors if selected, use selectedBackgroundView.backgroundColor


UIImageView

UIImageView with a chevron image on the right-hand side. Preferably in the storyboard, but it can easily be managed in code by attaching it to the content view.


color a uiview with the same bounds as the cell and add it as a subview




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    UIView *colorView = [cell viewWithTag:200];
    if(!colorView){
        colorView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 44)] autorelease];
        colorView.backgroundColor = [UIColor redColor];
        colorView.tag = 200;
        [cell addSubview:colorView];
    }

    return cell;
}


then add all of your subviews to that colored view.


The accepted answer didn't work for me, but the following code worked :

[cell.contentView setBackgroundColor:[UIColor whiteColor]];

Hope this helps someone


This drove me nuts. I tried a number of different approaches in the cellForRowAtIndexPath method. I finally found the way to do it is to override the willDisplayCell method like so:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row % 2) {
        cell.backgroundColor = [UIColor whiteColor];
    } else {
        cell.backgroundColor = [UIColor colorWithRed:225.0/255.0 green:225.0/255.0 blue:225.0/255.0 alpha:1.0];
    }
}


Being that its a UIView subclass, you should be able to set the background color of the accessoryView:

cell.accessoryView.backgroundColor = [UIColor clearColor];

or:

cell.accessoryView.backgroundColor = [UIColor colorWithHexString:@"#FFFFCC"];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜