views:

2248

answers:

3

I need to change the color of disclosureIndicatorView accessory in the tableView cell. I think there are two way to get this done. But not able to figure out which onez the optimum. So here is what I think I can do.

There is a property of tableViewCell - accessoryView. So I can use setAccessoryView:(UIView *)view and pass view as the imageView holding the image that I want.

I have written a utility class which creates the content view (stuff like bg color, adding other stuffs etc) for my cell and I add this content view to the cell in tableView delegate. The other option is to draw a UIImage overriding the drawRect method of CustomContentView utility class.

Performing option 1 - I can get the things done the apple way. Just give them the view and they do the rest. But I guess adding a new UIView object to every row might turn out to be a heavy obj allocation and decreasing the frame rate. As compared to just a UIImage object in my contentView. I believe UIImage is lighter than UIView.

Please throw some light people and help me decide over it.

Thanks

+2  A: 

But I guess adding a new UIView object to every row might turn out to be a heavy obj allocation and decreasing the frame rate. As compared to just a UIImage object in my contentView. I believe UIImage is lighter than UIView.

Drawing an image directly will almost certainly have better performance than adding a subview. You have to determine if that extra performance is necessary. I've used a few accessory views for custom disclosure indicators on basic cells and performance was fine. However, if you're already doing custom drawing for the content rect, it might not be that difficult to do the accessory view also.

amrox
Thanks for the response. I will give a shot and post the stats for frame rate and objAllocation if possible
AJ
+6  A: 

If you're interested in drawing the indicator, instead of using an image file, here's code I worked out to do so:

// (x,y) is the tip of the arrow
CGFloat x = CGRectGetMaxX(self.bounds) - RIGHT_MARGIN;
CGFloat y = CGRectGetMidY(self.bounds);
const CGFloat R = 4.5;
CGContextRef ctxt = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(ctxt, x-R, y-R);
CGContextAddLineToPoint(ctxt, x, y);
CGContextAddLineToPoint(ctxt, x-R, y+R);
CGContextSetLineCap(ctxt, kCGLineCapSquare);
CGContextSetLineJoin(ctxt, kCGLineJoinMiter);
CGContextSetLineWidth(ctxt, 3);
// If the cell is highlighted (blue background) draw in white; otherwise gray
if (CONTROL_IS_HIGHLIGHTED) {
    CGContextSetRGBStrokeColor(ctxt, 1, 1, 1, 1);
} else {
    CGContextSetRGBStrokeColor(ctxt, 0.5, 0.5, 0.5, 1);
}
CGContextStrokePath(ctxt);

If you make a custom UIView subclass, do the above in the drawRect: method, and use that as your accessory view, you'll be able to make the color anything you want.

An accessory view (custom or UIImageView won't be a major performance problem as long as you are properly recycling UITableViewCell instances.

benzado
Can you tell me how CONTROL_IS_HIGHLIGHTED would be set? I mean, how does the cell know it's highlighted?
cannyboy
If you're drawing in/on a table view cell, I'm pretty sure UITableViewCell has a `highlighted` property.
benzado
I've been trying to use this code but for some reason I lose the sharp point where the two lines intersect and get a muted angle, any ideas?
tigermain
CGContextSetLineJoin() governs what happens at that point. Maybe you left it out? Or do you call it once and then draw something else which changes the current line-join setting?
benzado
A: 

How do you implement this code? Can you post a wider context (tableView:cellForRowAtIndexPath: method or were?)

Andrejus
Welcome to Stack Overflow! Please read the FAQ. You should post this question as a new question, linking to this question as appropriate.
benzado