ios7 UITableViewCell selectionStyle won't go back to blue - uitableview

Xcode 5.0, iOS 7 and updating an existing app. UITableView selected row is now gray, not blue.
From what I've read they changed the default selectionStyle to gray. But "Blue" is still an option in IB and UITableViewCellSelectionStyleBlue still exists. Checking out the new HIG, it doesn't look like they removed the blue and the "Settings" app still using blue cell selection.
I've tried setting the value in IB and in code, but no luck. Any ideas on what I need to do to get the blue selection style back?

There is only one selectionStyle in iOS7, to change you need to do this manually like below:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
....
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor colorWithRed:(76.0/255.0) green:(161.0/255.0) blue:(255.0/255.0) alpha:1.0]; // perfect color suggested by #mohamadHafez
bgColorView.layer.masksToBounds = YES;
cell.selectedBackgroundView = bgColorView;
....
return cell;
}

I know this has already been answered but the last thing I wanted to do was touch all of my cellForRowAtIndexPath methods. So, I used an appearance proxy in my App Delegate. I took #null's code above to set the selected background view and in the applicationDidFinishLaunching:withOptions: method I placed this code.
UIView *bgColorView = [[UIView alloc] init];
//the rest of null's code to make the view
[[UITableViewCell appearance] setSelectedBackgroundView:bgColorView];
Then to make the white text hi light:
[[UILabel appearanceWhenContainedIn:[UITableViewCell class], nil] setHighlightedTextColor:[UIColor whiteColor]];
This made a global change in my app. The appearance proxy was introduced in iOS5 and Mattt has a great article on how to use it at his NSHipster blog.

Probably it could help you. I have my custom cell and to make it selected with needed color I have overwrite setHighlighted and setSelected now it's look like that
#define IOS_7 (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1 ? YES : NO)
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
[self changeSelectionColorForSelectedORHiglightedState:selected];
// Configure the view for the selected state
}
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
[super setHighlighted:highlighted animated:animated];
[self changeSelectionColorForSelectedORHiglightedState:highlighted];
}
- (void)changeSelectionColorForSelectedORHiglightedState:(BOOL)state
{
if (IOS_7) {
if (state) {
self.contentView.backgroundColor = [UIColor blueColor];
}
}
}

I know I'm really late to the party, but I'll offer my IOS10 work around as well. Don't touch any of your other code, but add:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.backgroundColor = [UIColor blueColor];
cell.textLabel.textColor = [UIColor whiteColor];
... whatever else you do ...
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
cell.textLabel.textColor = [UIColor blackColor];
}

Related

Subclassed UItableViewCell selection

I have subclassed a UITableViewCell so that I can increase the scrolling performance which has worked out great for me so far.
In my subclass I have a method called seSelected that looks like this
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
if (selected) {
self.backgroundColor = [UIColor lightGrayColor];
}else{
self.backgroundColor = [UIColor whiteColor];
}
}
I would like to know how to make it so that if I touch the same cell it deselects the cell and changes the color back to white? I have tried a few different if statments in setSelected but nothings working.
Use this delegate method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
and call here method which manage selected and unselected cell.
One approach could be to set the tag on the cell and then use it to set the background color.
typedef enum {
CellSelected = 0,
CellDeselected
} CellSelection;
While creating the cell set the tag to "CellDeselected"
cell.tag = CellDeselected;
And then when cell is tapped just check which color you want to set in the background.
switch (customCell.tag) {
case CellSelected:
self.backgroundColor = [UIColor lightGrayColor];
break;
case CellDeselected:
self.backgroundColor = [UIColor whiteColor];
break;
default:
break;
}
customCell.tag = !customCell.tag;
Do like this...
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
str = [YourArray objectAtIndex:indexPath.row];//str is a string variable
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIndentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIndentifier];
}
cell.textLabel.text = [reminder objectAtIndex:indexPath.row];
cell.backgroundColor = [UIColor whiteColor];
if ([str isEqualToString:[reminder objectAtIndex:indexPath.row]])
{
cell.backgroundColor = [UIColor grayColor];
}
return cell;
}
You can use for this for your custom cell.....
use UITableViewCell method: [cell setSelectedBackgroundView:myBgColorView];.
Apple Documentation.
Example:
UIView *myBgColorView = [[UIView alloc] init];
myBgColorView.backgroundColor = [UIColor greenColor];
[cell setSelectedBackgroundView:myBgColorView];
set cell style:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
and your code will work. But, you set color only selected cell.
If you need set color when cell is pressed, override this method:
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated

Cannot change UITableView blue highlight color

I set:
cell.selectionStyle = UITableViewCellSelectionStyleGray;
and use the code to highlight a row:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection: 0];
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone
The highlight color always blue even I set to gray. If I set:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
it works fine and no highlight. But just not work with:
cell.selectionStyle = UITableViewCellSelectionStyleGray;
It just show blue color instead of gray color. Any idea? Thanks.
Implement it as follows:-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath {
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
}
OR
Set the selectedBackgroundView's color as what you want in your custom tableview cell (which is a subclass of UITableViewCell):
UIView * selectedBackgroundView = [[UIView alloc] initWithFrame:self.frame];
[selectedBackgroundView setBackgroundColor:[UIColor redColor]]; // set color here
[self setSelectedBackgroundView:selectedBackgroundView];
or you can configure it in -tableView:cellForRowAtIndexPath: method:
//...
[cell setSelectedBackgroundView:selectedBackgroundView];
//...
Note that in iOS 7, using
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
will not work as expected, because in iOS 7 this is now gray, even if you pass the constant above. See:
https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html#//apple_ref/c/tdef/UITableViewCellSelectionStyle
Just add this in your method its work for me
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
....
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor colorWithRed:(76.0/255.0) green:(161.0/255.0) blue:(255.0/255.0) alpha:1.0]; // perfect color suggested by #mohamadHafez
bgColorView.layer.masksToBounds = YES;
cell.selectedBackgroundView = bgColorView;
....
return cell;
}
if you have any question feel free to ask
Make sure to do this sort of configuration either in InterfaceBuilder or in the cellForRowAtIndexPath: method.
Make a global search of "UITableViewCellSelectionStyleBlue" to ensure you have not made a typo.

Clear answer on how to Mask a UIView as a UITableViewCell selectedBackgroundView

I've read and tried a few answers I have found on StackOverflow. I've also read and tried a few things from blogs, but nothing seems to accomplish what I am looking for.
I create a UIView and set it's background color to my desired UITableViewCell selection color (instead of the standard blue or gray selection colors). I add this UIView to my cell's selectedBackgroundView and this works fine, my cell changes to the desired color on user selection.
This method works great on Plain UITableViews; not so well on Grouped. On a grouped UITableView, the 1st and last cell do not conform to clip / mask bounds as demonstrated in the below screenshots.
I know there is no way to round just the top-left and top-right corners only.
I want to do this strictly by code, without images.
Question
Does anyone know of a nice little work around to change the selectedBackgroundView color of a UITableViewCell using only the UIView and not images AND to make the 1st and last cell conform to the rounded corner boundaries?
Example
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * CellIdentifier = #"Cell";
WCSBadgedCell * cell = [[WCSBadgedCell alloc] initWithStyle:UITableViewCellStyleSubtitle andBadgeStyle:0 reuseIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[WCSBadgedCell alloc] initWithStyle:UITableViewCellStyleDefault andBadgeStyle:0 reuseIdentifier:CellIdentifier];
}
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:DARKBROWN];
[bgColorView setClipsToBounds: YES];
[cell.layer setMasksToBounds:YES];
[cell setSelectedBackgroundView:bgColorView];
[cell.textLabel setText: #"Testing a Cell"];
return cell;
}
Screenshots
Solution
I Accepted CodaFis answer because he added a comment which pointed to a pretty nice (yet lengthy) solution. I had to do quite a bit of revamping, but in the end, I now have the selectedBackgroundView's I needed which round the corners on the 1st and last cells, thanks again!
Here is a n example of how I achieved this.
I assume that you are using a UITableViewCell subclass because of the complexity of your cell. This is how I've been doing it:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]))
{
self.clipsToBounds = YES;
UIView* bgView = [[UIView alloc] init];
bgView.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.25f];
self.selectedBackgroundView = bgView;
//other code
}
return self;
}
This produces a sort of dark grey overlay on the cell, not images required!
In your case, the exact color of your selected cell (thanks to the handy dandy Digital Color Meter) would be
[UIColor colorWithRed:106.0f/255.0f green:51.0f/255.0f blue:6.0f/255.0f alpha:1.0f];
and the white text would be
- (void)setSelected:(BOOL)sel animated:(BOOL)animated
{
[super setSelected:sel animated:animated];
if (sel)
{
self.textLabel.textColor = [UIColor whiteColor];
}
else
{
self.textLabel.textColor = [UIColor colorWithRed:(105.f/255.f) green:(50.f/255.f) blue:(6.f/255.f) alpha:1.f];
}
}
I had the same problem, and fixed it by overriding -(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated in my UITableViewCell subclass and setting no selection style when creating the cell
//set no selection style in the cell
...
[self setSelectionStyle:UITableViewCellSelectionStyleNone];
...
//override setHighlighted to manually set the regular/highlighted background colors
-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
[super setHighlighted:highlighted animated:animated];
if(highlighted){
self.backgroundColor = [UIColor greenColor];
}
else {
self.backgroundColor = [UIColor redColor];
}
}

iOS / iPhone - setting the color of an arbitrary row

I'm trying to set the color of the selected row in a UITableView, using this code:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor redColor];
}
}
The problem is, it's setting the color on all the rows. Furthermore, when I do select a specific row, it highlights that row in blue. How do I get it to just highlight the selected row, in red?
The easiest way to me is to subclass UITableViewCell and overwrite the setSelected: method (leave everything else the same if you need to)
some sample code from an old project:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
UIView *bg = [[UIView alloc] initWithFrame:self.frame];
bg.backgroundColor = [UIColor colorWithRed:.916 green:.9648 blue:.9844 alpha:1.0];
self.selectedBackgroundView = bg;
[bg release];
// Configure the view for the selected state
}

UITableViewCell selected row's text color change

I am having a tableview and I want to know that how can I change the selected row's text color, say to Red ? I tried by this code :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell= [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:nil] autorelease];
cell.text = [localArray objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
cityName = [localArray objectAtIndex:indexPath.row];
UITableViewCell* theCell = [tableView cellForRowAtIndexPath:indexPath];
theCell.textColor = [UIColor redColor];
//theCell.textLabel.textColor = [UIColor redColor];
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
(1) When I select any row then text color changed to the red but when I select another then previously selected row's text remains red. how can I solve this ?
(2) When I scroll the table text color change to the black color how to solve this ?
Thanks..
Do this in tableView:cellForRowAtIndexPath::
cell.textLabel.highlightedTextColor = [UIColor redColor];
(And don't use cell.text = ... anymore. It has been deprecated for almost 2 years now. Use cell.textLabel.text = ... instead.)
As Raphael Oliveira mentioned in the comments, if the selectionStyle of your cell equals UITableViewCellSelectionStyleNone this won't work. Check Storyboard as well for the selection style.
If you want to change the text colour only, with out changing cell background colour. you can use this.Write this code in in cellForRowAtIndexPath method
UIView *selectionColor = [[UIView alloc] init];
selectionColor.backgroundColor = [UIColor clearColor];
cell.selectedBackgroundView = selectionColor;
cell.textLabel.highlightedTextColor = [UIColor redColor];
I had the same problem, try this!
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
for (id object in cell.superview.subviews) {
if ([object isKindOfClass:[UITableViewCell class]]) {
UITableViewCell *cellNotSelected = (UITableViewCell*)object;
cellNotSelected.textLabel.textColor = [UIColor blackColor];
}
}
cell.textLabel.textColor = [UIColor redColor];
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
That may be the solution to your (and mine) problem.
If you're already subclassing UITableViewCell, it's easier/cleaner to set the colors in the awakeFromNib method (assuming you're instantiating from a storyboard or xib).
#implementation MySubclassTableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
self.selectedBackgroundView = [[UIView alloc] initWithFrame:self.frame];
self.selectedBackgroundView.backgroundColor = [UIColor colorWithRed:0.1 green:0.308 blue:0.173 alpha:0.6];
self.customLabel.highlightedTextColor = [UIColor whiteColor];
}
#end

Resources