Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell accessoryType set, but accessoryView is nil. How do I set the accessoryView backgroundColor?

Here's how my cells are set up:

- (UITableViewCell *)tableView:(UITableView *)tableViewIn cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"subcategory";
    UITableViewCell *cell = [tableViewIn dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.textLabel.text = [[subcategories objectAtIndex:indexPath.row] title];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:16];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:16];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleGray;

    cell.contentView.backgroundColor = [UIColor clearColor];

    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.highlightedTextColor = [UIColor whiteColor];

    return cell;
}    

As one can see, I set the background of the contentView, and that works just fine - but when I try to do the same thing with backgroundView or accessoryView, nothing happens because backgroundView and accessoryView both seem to be nil.

When one sets the accessoryType rather than the accessoryView, how does one then go about setting the background of the accessory?

like image 694
Ben Collins Avatar asked Sep 15 '10 04:09

Ben Collins


2 Answers

From UITableViewCell Class Reference:

backgroundView

Discussion

The default is nil for cells in plain-style tables (UITableViewStylePlain) and non-nil for grouped-style tables UITableViewStyleGrouped). UITableViewCell adds the background view as a subview behind all other views and uses its current frame location.

accessoryView

Discussion

If the value of this property is not nil, the UITableViewCell class uses the given view for the accessory view in the table view’s normal (default) state; it ignores the value of the accessoryType property. The provided accessory view can be a framework-provided control or label or a custom view. The accessory view appears in the the right side of the cell.

These views will be nil until you define them, and so you can set their backgrounds as desired.

like image 128
vfn Avatar answered Nov 14 '22 21:11

vfn


Here is the reference from Apple:

@property(nonatomic, retain) UIView *accessoryView

If the value of this property is not nil, the UITableViewCell class uses the given view for the accessory view in the table view’s normal (default) state; it ignores the value of the accessoryType property. The provided accessory view can be a framework-provided control or label or a custom view. The accessory view appears in the the right side of the cell.

What I understand here is that if you set it, then the runtime will use it and ignore the accessoryType. But I suspect that the vice versa will be the same. I mean that if you set the accessoryType, then the value of the accessoryView will be still nil.

I think this is the design purpose. If you don't set the accessoryView but set the accessoryType, then the runtime has to decide what it has to do: If it sets the accessoryView, then the value of that property is not nil, then it has to ignore the accessoryType, which shouldn't be the case

like image 31
vodkhang Avatar answered Nov 14 '22 23:11

vodkhang