Show Selection Highlight on UITableView Section Header When Touched - ios

By default on a UITableView, when you touch a cell, it goes into selected mode, and is highlighted grey.
In our app, you can touch a header and it goes on to another page.
We want users to get the same kind of visual feedback when they touch a section, as when they they touch a cell. That is, it responds by going into a "selected" mode and making the background go a bit darker to indicate selection.
This does not occur when you touch a UITableView section. The section does not go into selection mode. It does not turn grey.
We've tried putting in a transparent button that spans a custom UIView with a background image and enabling "Reverses on Highlight", but the result is not very responsive and sucks.
What's the best strategy for implementing such functionality?

I would suggest subclassing UIGestureRecognizer to get the touchDown event, as pointed out in this post: Touch Down Gesture
You can then add the gesture recognizer to your UITableViewHeaderFooterView in viewForHeaderInSection and when it fires, grab the index path of the "selected cell." It will be null for the first section for some reason, but will have the correct indexPath.section for all other sections:
CGPoint point = [touchDown locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];
Then just change the background color of your header cell. You'll probably want to wipe out the color on your header when tapping another header or cell or whatever, so hold on to the cell whenever you change its background:
// declare an instance UITableViewHeaderFooterView earlier
if(header)
[[header contentView] setBackgroundColor:[UIColor lightGrayColor]]; // or whatever your default is
if(indexPath)
{
header = [self.tableView headerViewForSection:indexPath.section];
[[[self.tableView headerViewForSection:indexPath.section] contentView] setBackgroundColor:[UIColor greenColor]]; // or whatever color you want
}
else
{
header = [self.tableView headerViewForSection:0];
[[[self.tableView headerViewForSection:0] contentView] setBackgroundColor:[UIColor greenColor]];
}

Related

Unable to fetch UITableView Footer

I'm trying to change the background color of the Footer of a UITableView section. However, I'm not having any luck.
UITableViewCell *cell = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:i]];
cell.backgroundColor = [UIColor redColor];
cell.textLabel.textColor = [UIColor redColor];
cell.detailTextLabel.textColor = [UIColor redColor];
UITableViewHeaderFooterView *header = [_tableView footerViewForSection:i];
[header setBackgroundColor:[UIColor redColor]];
The first part - where I set the colors in the cell - is working. The second part - where I want to set the footer color - isn't. What am I doing wrong?
You need to set the color inside the cellForRowAtIndexPath method and inside the footerViewForSection method.
The table view will request cells, headers and footers as it needs them. For cells, chances are it will reuse the same cell which is why you cell color seems to work. However if you do some scrolling you will find it probably stops working.
Your header view does not work because you are asking for a view in your code, but that will not be the same view the table uses as it will have asked for its own.
So you should set the colors when the cell, header or footer is being requested.
Ideally the cell/header/footer configurations should be driven from your data model. e.g. Inside cellForRowAtIndexPath, you should create the cell appropriate for the data model entry for the index path. Sets its color based on the color required by the data model entry. All changes then are done on the data model and you reload the affected sections and/or rows.
Is -footerViewForSection: returning a value? My guess is it's returning nil. Are you providing the footer view to the table view in the delegate methods? If you're not, it doesn't exist. If you are, you can set the color then.

Custom UIPickerView with Custom Background color

I have created a sample picker-view which will display some details. Setting the Background color using:
_pickerView.backgroundColor = [UIColor redColor];
doesn't seems to be working . But I wish I could create a picker view like the below image
What I was trying to do is , the entire PickerView background color should not be white, it should fill with the UIColor which I am giving. Is that possible?? How it can be done ??
Addition to Nina's answer, below are some of the good custom Picker view controls which will be good to use in terms of performance and customizable.
http://www.cocoacontrols.com/platforms/ios/controls/cppickerview
http://www.cocoacontrols.com/platforms/ios/controls/afpickerview
http://www.cocoacontrols.com/platforms/ios/controls/v8horizontalpickerview (Horizontal PickerView)
I wrote a custom picker view that uses a UITableView as it's starting point. Feel free to customize it however you want.
https://github.com/derekju/DjuPickerView
You can change selected color by putting tableview cell in it.
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UITableViewCell *cell = (UITableViewCell *)view;
if( cell == nil ) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
[cell setBackgroundColor:[UIColor redColor]];
[cell setBounds: CGRectMake(0, 0, cell.frame.size.width -20 , 44)];
cell.textLabel.text = [self.userNameArray objectAtIndex:row];
cell.userInteractionEnabled = NO;
}
return cell;
}
It is hard to customize a UIPickerView.
You might opt to use it in its default appearance or trying to simulate a UIPickerView using another UIKit component such as UITableView. There are some libs that are using UITableView to simulate a more flexible UIPickerView.
UIPickerView has subviews. Assigning an image to a view and adding it to the subview at index 2, would change the background of pickerview.
I had had a same kinda problem :-) See my question here!! Customize UIPickerView's Skin with images
Or you can use AFPickerView
The API does not provide a way to change the style and color of selection dialer. You can change only inner view of the picker how u want to design.
As the UIPickerView don't have the UI_APPEARANCE_SELECTOR.
Check out below link will help you out.
Custom iOS UIDatepicker using UIAppearance

How to change background selected color storyboard static cells

I have an app with storyboard. In one scene I have a tableview with statics cell content. Is possible to change the background selected color to another color out of the default options (blue and gray)?
I know If I can change cell background color in forRowAtIndexPath but in this case I haven't any datasource function from tableview. I'm sure that it is possible from IB or another function that I can modify...
Thanks in advance!
You don't need to write neither a single line of code to accomplish that. You can do it all using the storyboard. Just do that:
Add a UIView to your UITableViewCell and link it to the selectedBackgroundView property of the cell (to find this property, you will need to drag the line from the "New Reference Outlet" and release it over the desired UITableViewCell)
Change the color of the UIView to the desired color of the selected state
You can do the same thing with the backgroundView property. Also, you can use a UIImageView to use a image, instead of the single color background of a UIView.
Here is a sample file using a custom UIViewController instead of a UITableViewController, but it works on both: http://uxp.com.br/downloads/CustomCell.zip
I had the same problem. The solution has two parts:
1) getting the cells, look at this.
2) changing the background color: you must create a UIView with the background color you need and set it as the selectedBackgroundView of the cell
For instance, I used this code in the viewDidLoad of the uitableviewcontroller:
UIView *backgroundSelectedCell = [[UIView alloc] init];
[backgroundSelectedCell setBackgroundColor:[UIColor colorWithRed:130/256.0 green:169/256.0 blue:171/256.0 alpha:1.0]];
for (int section = 0; section < [self.tableView numberOfSections]; section++)
for (int row = 0; row < [self.tableView numberOfRowsInSection:section]; row++)
{
NSIndexPath* cellPath = [NSIndexPath indexPathForRow:row inSection:section];
UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:cellPath];
[cell setSelectedBackgroundView:backgroundSelectedCell];
}
Here is a solution for those of us using Auto-Layout on X-Code 5+...
For the static cells, you can supposedly change the background color, but it won't work. Each individual cell, however, will automatically have a Content View inside it in IB. If we change the background of this Content View, it changes the cell background.
Just something to add to #Leandro Alves' answer, so we don't have to drag extra UIViews onto our project!

IOS uitableview cell selected background

I have a UItableview in my IOS app with some information in it. I changed the Selected background color to clearcolor using the following code:
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:[UIColor clearColor]];
[cell setSelectedBackgroundView:bgColorView];
[bgColorView release];
There is text and 2 images in the cell, I've build this using CGRectMake.
But When I select and hold a table cell the the images disappear behind what looks like a white background.
As you can see I'm holding the "Dacnusa sibrica" cell, how can I fix this?
If you want to disable the blue selection of cells you can also set the cell's selection mode instead of modifying the background:
cell.selectionStyle = UITableViewCellSelectionStyleNone
This simply disables the blue selection when tapping the cell but still allows the cell to be selected (and thus handled by code).
With Auto layout it is possible to define Selection=None (default is gray) in the Interface Builder.
have you used [cell.contentView addSubview:image]?
use [tableView deselectRowAtIndexPath:indexPath animated:YES];
for deselecting the cell

UILabel background color not updating in UITableView

I'm having a perplexing problem with a simple UILabel I put on a cell in a UITableView. I enter a separate view after tapping on a row, like many UITableViews. In there, I update the cell so that when I return to the rows, it should be updated, with this:
MyTableViewCell* cell =
(MyTableViewCell*) [mTableView cellForRowAtIndexPath:
[NSIndexPath indexPathForRow:mActiveRow inSection:0]];
cell.myLabel.text = #"New Value"; // updated text
cell.myLabel.backgroundColor = [UIColor greenColor]; // updated color
When I return, though, only the text is updated, not the color. When I scroll the row off the screen and return it refreshes properly via another path of code that has exactly the same code. Could there be some trigger to refresh specifically the background color of the UILabel? I'm not sure why the text will refresh but the color won't.
Neeeeeeevermind. I had overlooked the [TableView reloadData] method to refresh the cells. That worked perfectly.

Resources