Ccna final exam - java, php, javascript, ios, cshap all in one. This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
Sunday, May 27, 2012
How can I disable the UITableView selection highlighting?
When you tap a row in a UITableView, the row is highlighted and selected. Is it possible to disable this so tapping a row does nothing?
Further, make sure you either don't implement -tableView:didSelectRowAtIndexPath: in your table view delegate or explicitly exclude the cells you want to have no action if you do implement it.
I use this when I have a UIButton - or some other control(s) - hosted in a UITableViewCell and I want the user to be able to interact with the controls but not the cell itself.
***NOTE: As Tony Million noted above, this does NOT prevent the user from triggering tableView:didSelectRowAtIndexPath:. I get around this by simple "if" statements, most often testing for the section and avoiding action for a particular section. Another way I thought of to test for the tapping of a cell like this is:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // A case was selected, so push into the CaseDetailViewController UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; if (![cell selectionStyle] == UITableViewCellSelectionStyleNone) { // Handle tap code here } }
2.To do this for an entire table, you can apply the above solution to each cell in the table, but you can also do this:
[tableView setAllowsSelection:NO];
**In my testing, this still allows controls inside the UITableViewCell to be interactive.
3.To make a cell "read-only", you can simply do this:
To sum up what I believe are the correct answers based on my own experience in implementing this:
If you want to disable selection for just some of the cells, use:
cell.userInteractionEnabled = NO;
As well as preventing selection, this also stops tableView:didSelectRowAtIndexPath: being called for the cells that have it set. (Credit goes to Tony Million for this answer, thanks!)
If you want to disable selection for the whole table, use:
All you have to do is set the selection style on the UITableViewCell instance using either:
ReplyDeletecell.selectionStyle = UITableViewCellSelectionStyleNone;
or
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
Further, make sure you either don't implement -tableView:didSelectRowAtIndexPath: in your table view delegate or explicitly exclude the cells you want to have no action if you do implement it.
More info here and here
Because I've read this post recently and it has helped me, I wanted to post another answer to consolidate all of the answers (for posterity).
ReplyDeleteSo, there are actually 4 different answers depending on your desired result:
1.To disable the blue highlighting without changing any other interaction of the cell:
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
I use this when I have a UIButton - or some other control(s) - hosted in a UITableViewCell and I want the user to be able to interact with the controls but not the cell itself.
***NOTE: As Tony Million noted above, this does NOT prevent the user from triggering tableView:didSelectRowAtIndexPath:. I get around this by simple "if" statements, most often testing for the section and avoiding action for a particular section. Another way I thought of to test for the tapping of a cell like this is:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// A case was selected, so push into the CaseDetailViewController
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (![cell selectionStyle] == UITableViewCellSelectionStyleNone) {
// Handle tap code here
}
}
2.To do this for an entire table, you can apply the above solution to each cell in the table, but you can also do this:
[tableView setAllowsSelection:NO];
**In my testing, this still allows controls inside the UITableViewCell to be interactive.
3.To make a cell "read-only", you can simply do this:
[cell setUserInteractionEnabled:NO];
4.To make an entire table "read-only"
[tableView setUserInteractionEnabled:NO];
For me, the following worked fine:
ReplyDeletetableView.allowsSelection = NO;
To sum up what I believe are the correct answers based on my own experience in implementing this:
ReplyDeleteIf you want to disable selection for just some of the cells, use:
cell.userInteractionEnabled = NO;
As well as preventing selection, this also stops tableView:didSelectRowAtIndexPath: being called for the cells that have it set. (Credit goes to Tony Million for this answer, thanks!)
If you want to disable selection for the whole table, use:
tableView.allowsSelection = NO;
(Credit to Paulo De Barros, thanks!)
From the UITableViewDelegate Protocol you can use the method willSelectRowAtIndexPath
ReplyDeleteand return nil if you don't want the row selected.
In the same way the you can use the willDeselectRowAtIndexPath method and return nil if you don't want the row to deselect.
Try to type:
ReplyDeletecell.selected = NO;
It will deselect your row when needed.
We can write code like
ReplyDeletecell.selectionStyle = UITableViewCellSelectionStyleNone;
but when we have custom cell xib above line give warning at that time for
custom cell xib
we need to set selection style None from the interface builder