1

I have selected Selection = None from the storyboard scene of that static UITableViewCell. I also added this method:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 1:
            return nil;
            break;
    }
}

Which doesn't particularly set well because it warns of a method not returning a value.

The cell is still selectable, one time, briefly and then no more. The cell is set up like this:

case 1:
    cell.textLabel.text = @"Search";
    break;

and the didSelectRowAtIndexPath like so:

case 1:
    NSLog(@"Unselectable Cell");
    break;

Am I missing anything else?

Rishil Patel
  • 1,977
  • 3
  • 14
  • 30
marciokoko
  • 4,988
  • 8
  • 51
  • 91
  • Take a look at this http://stackoverflow.com/questions/190908/how-can-i-disable-the-uitableview-selection-highlighting – Josip B. Jul 16 '13 at 05:03

4 Answers4

4

cell.userInteractionEnabled = NO;

Eugene Pinchuk
  • 546
  • 3
  • 14
  • Do i eliminated the willSelectRowAtIndexPath method I added and just add your code to the switch case for that cell in my cellForRowAtIndexPath? – marciokoko Jul 16 '13 at 03:02
  • Disabling user interaction works, but it will disallow it for any view contained inside the cell, so if it has a button inside, that button won't respond anymore. – Can Jul 16 '13 at 03:19
2

Selection is the state right after being highlighted, if you press on a cell and it turns blue, it's being highlighted, when you release it, it has been selected.

Therefore, if you can't highlight the cell, you can't select it. With that in mind, you can do:

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
    // I'm assuming you just want it for the row 1, right?
    if (indexPath.row == 1)
    {
        // You ain't getting selected today, son.
        return NO;
    }

    return YES;
}
Can
  • 8,502
  • 48
  • 57
  • This forced deselection would be at the didSelectRowAtIndexPath? And I cant really use either the shouldHighlightRowAtIndexPath or the one I posted for selection for only one cell row, right? Because I will never be able to return a different value for just one cell index path, right? – marciokoko Jul 16 '13 at 02:57
  • What do you mean "Forced deselection" by doing the above or below methods you will not be able to select it at all – heinst Jul 16 '13 at 03:00
  • Ok, sorry, I misread the original question, forget about the "forced deselection", that was me just being silly. You can use the code I gave above to simply disable highlighting (and therefore) selection on the row you want, you don't need anything else. – Can Jul 16 '13 at 03:18
1

What I do is I do something like so:

- (UITableViewCell *)tableView:(UITableView *)tmpTableView cellForRowAtIndexPath:   (NSIndexPath *)indexPath
{
tmpTableView.scrollEnabled = NO;
static NSString *CellIdentifier = @"MainCell";
UITableViewCell *cell = [tmpTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (nil == cell)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    if(indexPath.row == 0)
    {
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.userInteractionEnabled = NO;
        cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
        cell.textLabel.numberOfLines = 4;
        cell.textLabel.textColor = [UIColor grayColor];
        cell.textLabel.font = [UIFont systemFontOfSize:16.0];
        cell.textLabel.text = [self.answerArray objectAtIndex:indexPath.row];
        [cell.textLabel setAdjustsFontSizeToFitWidth:YES];
        return cell;
    }
}
cell.textLabel.font = [UIFont systemFontOfSize:18.0];
cell.textLabel.text = [self.answerArray objectAtIndex:indexPath.row];

return cell;
}

You could go line by line, of course this would only work for tables that dont dynamically change in number of cells.

heinst
  • 8,520
  • 7
  • 41
  • 77
1

You can also try this

tableView.allowsSelection = NO;

another way of doing this

cell.selectionStyle = UITableViewCellSelectionStyleNone;

one more

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Hemant Singh Rathore
  • 2,153
  • 1
  • 24
  • 38