4

I might be going about this all wrong, so hopefully someone will tell me what I should be doing.

I have constructed a table view to act as a legend and color picker. In the AccessoryView slot, I places a custom view that is just a colored box. User will show the legend, so they know what colors mean what, and they can pick on a color to then 'colorize' a drawing by assigning that color to objects. All of that works just fine.

What I'm having a problem with is the selected row style. When I select a row from the legend, the cell goes blue, like it should, but my AccessoryView has now disappeared. I don't want this to happen. However, I don't know what to set to make it not disappear. Keep in mind, I still want a row to show up as 'selected'. But any way that I do that, my accessory view disappears (most likely is hidden by the selected color).

Here's how I'm setting the accessory view right now.

CGRect colorBox = CGRectMake(0, 0, 30, 30);
UIView *colorView = [[UIView alloc] initWithFrame:colorBox];
colorView.backgroundColor = [self colorWithHexString:[selOption valueForKey:@"qoColor"]];
cell.accessoryView = colorView;
CrystalBlue
  • 1,793
  • 5
  • 18
  • 27
  • You are using code like `cell.accessoryView = myColorBox;` to set the accessory, right? – benzado Jun 24 '11 at 16:21
  • You're not doing anything obviously wrong. Are you using a stock UITableViewCell or a custom subclass? Try creating a minimal project that just puts a custom accessoryView in a table, and see if that has the same problem. – benzado Jun 24 '11 at 16:42
  • where do you set the accessory view? that might be the issue – aporat Jun 24 '11 at 17:01
  • I set the accessory view in the Table View's tableView cellForRowAtIndexPath, where the cell is being added. And this is a stock UITableViewCell where I'm setting the cell's TextLabel and AccessoryView. – CrystalBlue Jun 27 '11 at 15:30

1 Answers1

7

You can use a UIImageView instead of a UIView, which won't disappear when the cell is selected. You can either make tiny .png thumbnails for each color (if there aren't a lot) or you can create them dynamically in your cellForRowAtIndexPath delegate method, ala:

UIImageView *colorView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];

CGRect rect = CGRectMake(0.0f, 0.0f, 30.0f, 30.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [[self colorWithHexString:[selOption valueForKey:@"qoColor"]]; CGColor]);
CGContextFillRect(context, rect);

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

[colorView setImage:image];

[cell setAccessoryView:colorView];
[colorView release];
Keller
  • 17,051
  • 8
  • 55
  • 72