Cannot change UITableView blue highlight color - ios

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.

Related

UITableViewCell cannot be set transparent entirely with check box

I use below code to try to make tableview cell background clear colour when selected and it works without checkbox but when there is it's not clear colour totally.
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
cell.tintColor = [UIColor redColor];
UIView *selectionView = [UIView new];
selectionView.backgroundColor = [UIColor clearColor];
[[UITableViewCell appearance] setSelectedBackgroundView:selectionView];
As you can see from below picture, it's only clear under checkbox but not entire cell. But when there's no checkbox (not editing mode) it's entirely clear. How can I fix this? Thanks in advance.
If I remove the above code, the result will look like this. The white background will cover entire cell. I don't know why the above code can just set clear background to the area beneath checkbox?
add this method
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setBackgroundColor:[UIColor clearColor]];
}
Need to set clear background of cell and cell'contentView.
how about setting the selection style to none instead of blue?
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
I have find solution, We can edit below code
- (void)viewDidLoad {
[super viewDidLoad];
//Add below to line
[self.tblContact setAllowsMultipleSelectionDuringEditing:YES];
[self.tblContact setEditing:YES animated:YES];
//tblContact is tableView Outlet
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//ContactTableViewCell is xib file of UITableViewCell
static NSString *identifier = #"ContactTableViewCell";
ContactTableViewCell *cell = (ContactTableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ContactTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
cell.tintColor = [UIColor redColor];
UIView *selectionView = [UIView new];
selectionView.backgroundColor = [UIColor clearColor];
[[UITableViewCell appearance] setSelectedBackgroundView:selectionView];
cell.backgroundColor = [UIColor clearColor];
return cell;
}

Tableview cell separating line disappears when tapped

I have a UITableView with custom tableview cells with the separator line between each cell. I recently began implementing multi cell selection when in editing mode. In order to have the blue circled checkmarks when each cell is selected, I changed cell selectionStyle from UITableViewCellSelectionStyleNone to UITableViewCellSelectionStyleDefault. To get rid of the gray shade when the cell is selected I just implemented a white background like this:
UIView * cellBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
cellBackgroundView.backgroundColor = [UIColor clearColor];
cell.multipleSelectionBackgroundView = cellBackgroundView;
cell.selectedBackgroundView = cellBackgroundView;
The problem is that when I'm not in edit mode, the separator line disappears whenever the cell is selected, but when I select a cell in edit mode, the line remains. Any idea how to resolve this so the separator always remains?
try following:
Add this lines
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[tableView reloadRowsAtIndexPaths:#[indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
at the begining of didSelectRowAtIndexPath
this is weird bug which exists since iOS7.0
I know that the cell separator disappearing is a common issue, but none of the current solutions seemed to solve my problem so I came to the conclusion that I would need to toggle my selectionStyle to UITableViewCellSelectionStyleNone when my tableView is not in editing mode and UITableViewCellSelectionStyleBluewhen I'm in editing mode. I was able to achieve this by calling the following in my view controller containing my table view:
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(tableView.editing)
{
[((customTableViewCellClass*)[tableView cellForRowAtIndexPath:indexPath]) changeSelectionStyle:YES];
}
else
{
[((customTableViewCellClass*)[tableView cellForRowAtIndexPath:indexPath]) changeSelectionStyle:NO];
}
return indexPath;
}
Then in my custom tableviewcell class I call the following:
-(void) changeSelectionStyle: (BOOL) selected
{
if(selected)
{
self.selectionStyle = UITableViewCellSelectionStyleBlue;
}
else
{
self.selectionStyle = UITableViewCellSelectionStyleNone;
}
}
In cellForRowAtIndexPath I left the code that changed the view for multipleSelectionBackgroundView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIView * cellBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
cellBackgroundView.backgroundColor = [UIColor clearColor];
cell.multipleSelectionBackgroundView = cellBackgroundView;
cell.selectedBackgroundView = cellBackgroundView;
}

Changing background color for selected row on first load of table view iphone

Hi I am developing small IOS application in which I am loading some data inside tableview. So what I want to do I want to make first row as default selected row with some selected background color. So I tried following things
// inside viewDidLoad make first row as selected row
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
[_tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:0];
// inside cellForRowAtIndexPath set some selection color
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [customColor colorWithHexString:#"91979755"];
[cell setSelectedBackgroundView:bgColorView];
}
But it is not working. Any one know how to do this? Need some help. Thank you.
Try this code,put this in cellForRow method:
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor cyanColor];
[cell setSelectedBackgroundView:bgColorView];
And for specific row put this in if condition.
Hope it works.
self.indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[_tabelView selectRowAtIndexPath:self.indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
// inside cellForRowAtIndexPath set some selection color
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(self.indexPath.row==indexPath.row){
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [customColor colorWithHexString:#"91979755"];
[cell setSelectedBackgroundView:bgColorView];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.indexPath=indexPath;
}

ios7 UITableViewCell selectionStyle won't go back to blue

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];
}

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