1

How can one disable selection on all UITableviewCell's except for one in a UITableview.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    // Deselect row
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    // NSLog(@"Row Selected = %li",(long)indexPath.row);

    self.selectedInvitedUserId = [[self.viewOrderUserArray valueForKey:@"InvitedUserId"] objectAtIndex:[indexPath row]];
    NSLog(@"Row selectedInvitedUserId = %@",self.selectedInvitedUserId);

    if( self.selectedInvitedUserId && [indexPath row] != 0 )
        {
            if ([[self.viewSelectedItemsArray lastObject] count] == 0)
            {
                [self performSegueWithIdentifier:@"delimenuFromViewOrderList" sender:self];
            }
            else
            {
                [self performSegueWithIdentifier:@"viewOrderSummaryFromOrderDetails" sender:self];
            }
        } 
}

I know of this to disable all cells.

cell.selectionStyle = UITableViewCellSelectionStyleNone;
CapeFurSeal
  • 183
  • 2
  • 13
  • Are you using a custom cell ? @CapeFurSeal – Haroon Oct 13 '14 at 10:20
  • Do you want to disable all cells except one Right ? – Haroon Oct 13 '14 at 10:25
  • no just a normal tableview... – CapeFurSeal Oct 13 '14 at 10:25
  • @Haroon > Yes you are 100% correct – CapeFurSeal Oct 13 '14 at 10:27
  • @CapeFurSeal if you downvoted my answer please explain why rather than just marking it as incorrect. I can't solve your issue if I don't know the problem – Simon McLoughlin Oct 13 '14 at 11:03
  • never down voted anyone;s answer although your answer does not help me as I want to enable only one cell in the uitableview to be selectable.. – CapeFurSeal Oct 13 '14 at 11:18
  • @CapeFurSeal ok fair enough. But that is exactly what my code shows. I've used this many times to make my cells not selectable – Simon McLoughlin Oct 13 '14 at 11:26
  • Perhaps you didn't read the note under my code mentioning that there is 2 steps to this, 1. make sure the rows don't highlight. 2. check which row has been tapped. This will make which ever one you like selectable and the others not selectable – Simon McLoughlin Oct 13 '14 at 12:18
  • possible duplicate of [UITableview: How to Disable Selection for Some Rows but Not Others](http://stackoverflow.com/questions/2267993/uitableview-how-to-disable-selection-for-some-rows-but-not-others) –  Feb 12 '15 at 05:39
  • See http://stackoverflow.com/questions/2267993/uitableview-how-to-disable-selection-for-some-rows-but-not-others –  Feb 12 '15 at 05:39

2 Answers2

2

You need to do this in the

(UITableViewCell *)tableView:cellForRowAtIndexPath:

callback. This is a setting on a cell that will be executed when it is tapped. It is not something you should try to manually manage

like so:

// Where indexPath != 0 points to the row you want to enable
if( self.selectedInvitedUserId && indexPath != 0 )
{
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
else
{
    [cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
}

Note

This will only stop it being highlighted. You will need to check inside the didSelectRowAtIndexPath: callback if its the correct row.

Simon McLoughlin
  • 8,293
  • 5
  • 32
  • 56
  • @FahimParkar I think you've misunderstood my code. I wasn't suggesting that it was for one user. The OP asked a question and gave sample code using `selectedInvitedUserId` in an if statement to make a decision. I simply copied something similar in my answer to him. I will however change it to exactly what the OP had, with a tweak for my code – Simon McLoughlin Oct 13 '14 at 13:20
  • yes, my mistake... I got... that's why removed the downvote and upvoted... but I would say not to use UITableViewCellSelectionStyleBlue and handle click action in didSelectRowAtIndexPath. See my answer for more details. – Fahim Parkar Oct 13 '14 at 13:24
  • @FahimParkar ah ok, no problem, thanks for removing it. If you set all the cells to `UITableViewCellSelectionStyleNone` the user won't receive any indication that the cell was tapped. The OP may not want blue but thats up to him to decide or maybe ask himself. What my code has done is, it will only highlight in blue the row the OP wishes to be clickable and not the rest. The OP has largely already figure out what you have posted but has asked how to display only one, implying he is fine with how its running (defaulted to blue), but simply wants to prevent it happening on all cells – Simon McLoughlin Oct 13 '14 at 13:30
  • **NO**, with `cell.selectionStyle = UITableViewCellSelectionStyleNone;` just selection is disabled.. you can tap on cell and didSelectRowAtIndexPath is invoked too... – Fahim Parkar Oct 13 '14 at 13:32
  • @FahimParkar yes, I have a note in my answer to that fact also – Simon McLoughlin Oct 13 '14 at 13:33
  • has someone else downvoted my answer, again without any indication as to why? if you feel something is incorrect feel free to downvote but also leave a comment. Clearly You think I have done something incorrect, if I have I would also like to know what this is – Simon McLoughlin Oct 13 '14 at 13:34
  • for me its not wrong, just I would say not to use `UITableViewCellSelectionStyleBlue` bcz that will make cell background as blue when clicked... that it... – Fahim Parkar Oct 13 '14 at 13:35
  • @downvoter the OP has marked this as the top answer meaning it solved their issue. If you feel there is an issue with this answer please explain it so I can fix it. If not please remove it as several people including the OP disagree. Thank you – Simon McLoughlin Dec 09 '14 at 11:46
  • don't get panic with downvoters. Its their duty... :D – Fahim Parkar Dec 09 '14 at 11:59
  • @FahimParkar just bugs me is all, I no it doesn't actually mean anything in the long run. Just annoy's me when I get an anonymous downvoter and have no idea why – Simon McLoughlin Dec 09 '14 at 12:03
0

Below is what I would suggest.

in cellForRowAtIndexPath keep cell.selectionStyle = UITableViewCellSelectionStyleNone; alive for all cell.

Now you must be making action on didSelectRowAtIndexPath. There do like below.

if( userId == your_ignore_userid )
{
    // don't do anything
}
else
{
    // do action - like go to another view controller
}

Reason why not to use UITableViewCellSelectionStyleBlue in cellForRowAtIndexPath is because if user click on that cell (which you don't want to allow), background of that cell will become blue which will go against your app design and it will look ugly.

Hence I would suggest you to handle action in didSelectRowAtIndexPath.

Let me know if you have further questions.

Note:

With cell.selectionStyle = UITableViewCellSelectionStyleNone; just cell selection is disabled.. You can tap on cell and didSelectRowAtIndexPath gets invoked too...

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276