Customizing properties of subclassed UITableViewCell when selected - iphone

I am having trouble customizing the look and behavior of a subclassed tableview cell when it enters selected state.
My cell has three labels I added to its content view in the initWithStyle: method as such:
cell1Label = [[UILabel alloc] initWithFrame:
CGRectMake(75.0f, 12.0f, 67.0f, 12.0f)];
cell1Label.backgroundColor = [UIColor clearColor];
cell1Label.textColor = [UIColor blackColor];
cell1Label.shadowColor = [UIColor whiteColor];
blah, blah, blah...
[self.contentView addSubview:cell1Label];
Then, I put a black overlay on top of the background in the setSelected:(BOOL)selected animated:(BOOL)animated method within the subclass:
UIView *backgroundView = [[UIView alloc] initWithFrame:
CGRectMake(0.0f, 0.0f, 150.0f, 70.0f)];
backgroundView.backgroundColor = [UIColor colorWithRed:
0.0 green:0.0 blue:0.0 alpha:0.4];
self.selectedBackgroundView = backgroundView;
The problem start here. Because I want to keep my UILabel readable when the cell is selected, I need to change their textColor and shadowColor. However, I cannot seem to find a good place to do this.
If I put the code in the setSelected:(BOOL)selected animated:(BOOL)animated nothing happens; I can only seem to add changes to the selectedBackgroundView.
I also tried using the didSelectRowAtIndexPath: and didDeselectRowAtIndexPath: TableView delegate methods as such:
CustomDataCell* selectedCell = (CustomDataCell*)[tableView
cellForRowAtIndexPath:indexPath];
selectedCell.cell1Label.shadowColor = [UIColor lightGrayColor];
selectedCell.cell1Label.textColor = [UIColor blackColor];
This method, however, has some issues when cells leave the visible area. Namely, if I select a cell then it leaves the visible area, its text properties do not change back to their normal state when I select another cell. The black background disappears as it should, but the new textColor and shadowColor I assigned to the selected state persists.
What is the best, most reliable way to handle selected (and possibly other) states of subclassed UITableViewCells?
I am using ARC; never use IB; on Xcode 4.6 and iOS 6.1 SDK.

Use the setHighlighted:animated method of UITableViewCell to change your label color.
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Change you label text color here
//
// Edit Here
if (selected) {
// New Colors Here
}
else {
// Old Colors Here
}
}

When a cell is selected, it will set all of its labels (including ones you've added yourself) to their highlighted state. This means you can define the highlighted text color at initialisation and let the cell take care of it.
Probably at the moment the labels are being set to highlighted which is undoing any colour changes you are making yourself.
Also, a cell has a selectedBackgroundView property which you should be using instead of adding a new subview.

Related

selectedBackgroundView on UITableViewCell gets applied to all views in cell

I have a table cell being displayed that shows a users image, name and some text. The user's image is 50x50, but I want a border around it so I set the view to center the image and set the frame to 52x52 and then set the background color of that view to my border color. That shows a 1 pixel border around the image.
I also want to show a 30 pixel wide border on the right of the cell when the cell is selected. I've tried to do that by creating a UIView the size of the cell's frame, then adding a subview to that view with a UIView the width and background color I would like. I then set that view to the selectedBackgroundView of the cell.
The problem here is that the cell's selectedBackgroundView gets applied to the background of all views inside the cell. So when I select a cell, the images "border" gets set to the cell's selected background color, the other 30px "border" I'm adding gets changed to that background color also.
Code inside my cellForRowAtIndexPath:
cell = (UserCellView *) currentObject;
UIView *c = [[UIView alloc ] initWithFrame:CGRectMake(0, 0, 30, cell.frame.size.height)];
c.backgroundColor = [[UIColor alloc] initWithRed:64/255.0 green:64/255.0 blue:64/255.0 alpha:1.0];
UIView *v = [[UIView alloc ] initWithFrame:cell.frame];
v.backgroundColor = [[UIColor alloc] initWithRed:35/255.0 green:35/255.0 blue:35/255.0 alpha:1.0];
[v addSubview:c];
cell.selectedBackgroundView = v;
[c release];
[v release];
I'll assume that you haven't actually tested what's going on to form your analysis that it "gets applied to the background of all views inside the cell".
I did something like this:
#interface TestView : UIView {
}
#end
#implementation TestView
-(void)setBackgroundColor:(UIColor*)c {
// Breakpoint here.
NSLog("setBackgroundColor: %#",c);
[super setBackgroundColor:c];
}
#end
...
UIView * v = [[[UIView alloc] initWithFrame:(CGRect){{0,0},{20,20}}] autorelease];
v.backgroundColor = [UIColor magentaColor];
UIView * v2 = [[[TestView alloc] initWithFrame:(CGRect){{5,5},{10,10}}] autorelease];
v2.backgroundColor = [UIColor yellowColor];
[v addSubview:v2];
cell.selectedBackgroundView = v;
The end result is that -setBackgroundColor: is called from -[UITableViewCell _setOpaque:forSubview:] when the view is selected, with something like UIDeviceWhiteColorSpace 0 0 (i.e. [UIColor clearColor]).
Or, in other words, the background colour of some of the subviews are set to [UIColor clearColor] while the cell is selected, allowing selectedBackgroundView to show through. I think this happens because a common optimization is to give textLabel/detailTextLabel the table's background colour (e.g. white) so it draws faster, but this means the background colour has to be reset when the cell is selected.
The easiest fix is to use an image instead: a 1-by-1-pixel image of the correct colour in a UIImageView will work, if a bit messy. (I had this problem when drawing custom separator lines with 1-pixel-high UIViews, so I just included the separator into the background image.)
An alternative fix is to use a CALayer instead: Add a 52x52 sublayer to the UIImageView's layer, and set the sublayer's background colour. I'm pretty sure UITableViewCell simply walks the view hierarchy, so it should ignore custom layers. (The big disadvantage with layers is that they don't auto-size, which made them unsuitable for my purposes, and means the 30px right border won't auto-size.)
A workaround is to subclass the relevant views and ignore -setBackgroundColor: if it's equal to [UIColor clearColor].
A simple but obnoxious-to-maintain solution is to override setSelected:animated: and setHighlighted:animated: with implementations re-setting the various backgrounds you want. Something along the lines of:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
self.childView.backgroundColor = [UIColor blueColor]; // whichever you want
}
First add this to your file
#import <QuartzCore/QuartzCore.h>
Then turn your view into an image with...
UIView *rowView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 1.0, 60.0)];
rowView.backgroundColor = [UIColor colorWithRed:35/255.0 green:35/255.0 blue:35/255.0 alpha:1.0];
UIGraphicsBeginImageContext(rowView.bounds.size);
[rowView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *yourImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Then instead of adding a UIView to your cell, just add a UIImageView with "yourImage".
A simple solution if the affected view can be a custom subclass is to override -setBackgroundColor:
- (void)setBackgroundColor:(UIColor *)color
{
// Ignore requests and do nothing
}
Thus UITableViewCell's attempt to set the colour will go ignored. Code in the custom view which really does want to set the background colour needs to call super:
- (void)setColor:(UIColor *)color
{
[super setBackgroundColor:color];
}
(or could probably message the underlying CALayer directly)
you will need to customize the contentView of the cells and handle the delegate tableView:cellForRowAtIndexPath
See Posting Here

UITableViewCellSeparatorStyleNone does not hide blue separator line when selecting in UITableView

Before describing the problem, let me first point out that this is a distinct issue from this question.
The Problem
This screenshot was taken with a break set at tableView:didSelectRowAtIndexPath:, and as you can see in the simulator (far right of the image), there's a single-pixel blue line at the bottom of the selected cell. This is not the design asked for by the client, nor is it how this app used to behave: there should be no separator, even on selection.
How I Got Here
I'd initially designed this table view using custom UITableViewCell classes with corresponding nib (.xib) files and had no trouble with selections: the separator was hidden as desired. Predictably, scrolling was sluggish due to all the overhead from the view hierarchy, so I reworked the custom cells to use Loren Brichter's fast scrolling solution. Now scrolling is much faster, but I can't get rid of the separator for the life of me.
What I've tried
At the time of the screenshot above...
the table view has "Separator [None]" in IB.
the UIViewController that contains the table view has this line in viewDid Load: self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
As you can see in the screenshot, I inserted some unused lines to prove that separatorStyle is set as desired. Other testing confirms that tableView and self.tableView are equivalent pointers at that same breakpoint.
I've also tried setting tableView.separatorColor to black and to clear, all with the same result: the cells look right until a selection is made.
Manjunath: Here's the code I'm using to draw alternate backgrounds depending on whether the cell's been touched or not. You can see the difference—which is less subtle when animated—in the screenshot.
if(self.highlighted) {
textColor = [UIColor blackColor];
UIImage *bg = [UIImage imageNamed:#"image-cell-background_highlighted.png"];
[bg drawAtPoint:CGPointMake(0.0, 1.0)];
}
else {
UIImage *bg = [UIImage imageNamed:#"image-cell-background.png"];
[bg drawAtPoint:CGPointMake(0.0, 0.0)];
}
This gets called in UIImageCell.m in drawContentView:, a method inherited from Mr. Brichter's ABTableViewCell super class.
Chris,
Delving into ABTableViewCell, I see:
- (void)setFrame:(CGRect)f
{
[super setFrame:f];
CGRect b = [self bounds];
b.size.height -= 1; // leave room for the seperator line
[contentView setFrame:b];
}
Since the height of the cell is one pixel shorter than the actual cell, when the cell gets selected, that one-pixel line will bleed through in the color of the selection color. It may look like it's the separator, but it is actually the selection color.
To test, try to change that line above to be two pixels or more shorter to see what happens.
Update:
By making this change to the FastScrollingExample project's -rootViewController:
- (void)viewDidLoad
{
self.title = #"Fast Scrolling Example";
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[super viewDidLoad];
}
and commenting out:
// if(self.selected)
// {
// backgroundColor = [UIColor clearColor];
// textColor = [UIColor whiteColor];
// }
//
in -drawContentView to mimic what would happen if you didn't have the selection color showing through, then I get a screen shot like this:
alt text http://files.me.com/mahboud/7k656q
Look familiar?
How would you get around this? If you don't need to select cells, then disable cell selection. Otherwise, if you are selecting cells, then you should make the rect larger so the default selection color doesn't show through when you paint with your own selection color in -drawConentRect.
Try this:
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = NSLocalizedString(#"Cell",#"");
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (nil == cell)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}

How to change the border color of a Grouped UITableView

This concerns iPhoneOS-sdk-3.2
I am having difficulty changing the border color of a grouped UITableView. I can change the cell background color, separator color, text color, quite easily now, and the rounded corners clip correctly, even when highlighted with whatever colors I have chosen. However the surrounding border remains infuriatingly gray despite many different attempts.
I have read all of the related posts I can find via Google, let alone stackoverflow. I have seen Mike Akers' heroic PITA solution for UITableViewCell clipping -- this problem is solved for iPhoneOS 3.0 and it did not help me with the border.
I have tried both a programmatic and xib-based solution and both provide the same results.
I will share the programmatic version below:
I have a UIViewController subclass rather than a UITableViewController subclass to act as a UITableView delegate -- I chose this route as I am coding on the iPad and UITableViewController reportedly takes over the whole screen. loadView method of my UIViewController subclass:
- (void) loadView {
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[self.view release];
self.view.backgroundColor = [UIColor blackColor];
// add and configure UITableView
CGRect tableViewRect = CGRectMake(0., 0., 256., 768.);
myTableView = [[UITableView alloc] initWithFrame:tableViewRect style:UITableViewStyleGrouped];
// set the tableview delegate to this object and the datasource to the datasource which has already been set
myTableView.delegate = self;
myTableView.dataSource = self;
myTableView.sectionIndexMinimumDisplayRowCount = 1;
myTableView.backgroundColor = [UIColor clearColor];
myTableView.separatorColor = [UIColor whiteColor];
myTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
myTableView.opaque = NO;
// add the table view to our background view
[self.view addSubview:myTableView];
[myTableView release];
}
I found a solution. This behavior does appear to be iPhoneOS 3.2 specific as Apple added a backgroundView property for UITableView in iPhoneOS 3.2.
I tried [myTableView.backgroundView removeFromSuperView] and UITableView just replaced it with another.
Instead, my solution was to add:
myTableView.backgroundView.hidden = YES;

How do I fill the background color of a UITableViewCell?

How do I fill the background color of a UITableViewCell? I tried this code but it didn't work.
cell.backgroundColor = [UIColor redColor];
Try setting a backgroundView for UITableViewCell :
UIView *bgView = [[UIView alloc] init];
bgView.backgroundColor = [UIColor redColor];
cell.backgroundView = bgView;
// release object
[bgView release];
You can change the selection background view of UITableViewCell the same way.
cell.selectedBackgroundView = bgView;
Setting the background color though UITableViewCell's backgroundColor property only works in a Grouped table view. So if your table view is in the Plain style then it won't work.
You can of course set the background color of the UITableView's contentView. But then you probably have to do some additional work as the other subview (text labels and accessory views) have their own idea of background colors.
Take a look at the following snippet from the UITableViewCell documentation.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html
Note: If you want to change the background color of a cell (by setting the background color of a cell via the backgroundColor property declared by UIView) you must do it in the tableView:willDisplayCell:forRowAtIndexPath: method of the delegate and not in tableView:cellForRowAtIndexPath: of the data source. Changes to the background colors of cells in a group-style table view has an effect in iOS 3.0 that is different than previous versions of the operating system. It now affects the area inside the rounded rectangle instead of the area outside of it.
That should solve your problem.

How to change UIPicker Color ? iPhone

I have observed that UIPicker always remains in black color,
Is there any way to change the color of UIPicker & it's Selection Indicator?
Thanks for helping me.
I assume all you want to change is the color of the border of the picker, not of the region in the center with which the user interacts. In this case, do the following:
Create 4 "cover" UIViews and add them directly to the UIPicker, as in:
[picker addSubview: coverView];
Position these views over the top, bottom, left and right sides of the picker border. (You will need to experiment with sizes.) Set the backgroundColor of the coverViews to the color you want, and adjust the alpha to get the gradient shading from the picker. Again, this may take a bit of experimentation.
The alternative would be to create one big coverView that covers the entire picker, and override the - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event method so that your cover view did not intercept the touches meant for the picker.
You can create your own custom pickers, check the UICatalog sample project at apples site, they show how to make a custom picker, it might help you https://developer.apple.com/iphone/library/samplecode/UICatalog/
Create a single overlay image as Amagrammer suggested but instead of adding it to your view as an image, add it as a button. Then disable the interaction on the button and the picker will receive the touch events without the need to intercept or override anything.
You can do this by adding 5 ImageView also. First one is of size "Selection Indicator" and put it exactly over "Selection Indicator". Now change that imageView's alpha to 0.2 or whatever you want and also add image/color as per your choice. Now, you will see your custom "Selection Indicator". Also, same thing you can do for borders. Add each imageView to four sides of pickerview and make it's size equal to border size. Now fill the images/color you want in that ImageView.
This is the best solution I have found
http://www.inexika.com/blog/Customizing-UIPickerView-UIDatePicker
You'll need to create a new UIView with a UIImageView inside it and then set that as the Accessory for the cell. So you'll need to create a image just like the default accessory but in the color you want.
UIView* accessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 24, 50)];
UIImageView* accessoryViewImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"NEWIMAGE.png"]];
accessoryViewImage.center = CGPointMake(12, 25);
[accessoryView addSubview:accessoryViewImage];
[cell setAccessoryView:accessoryView];
[accessoryViewImage release];
[accessoryView release];
for changing text color here's the solution
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
CGRect imageFrame = CGRectMake(0.0, 0.0, 15, 15);
UIImageView *label = [[[UIImageView alloc] initWithFrame:imageFrame] **autorelease**];
if (row == 0)
{
label.backgroundColor = [UIColor redColor];
}
if (row == 1)
{
label.backgroundColor = [UIColor blueColor];
}
if (row == 2)
{
label.backgroundColor = [UIColor blackColor];
}
return label;
}