4

Issue: I only want to show the selected cells with checkmark. I don't want the grey highlight. I tried:

cell.selectionStyle = UITableViewCellSelectionStyleNone

but didn't work.

Here is the code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

     if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];                           
      }

     ProfileSelection *profile = [self.profileSelections objectAtIndex:indexPath.row];
     cell.textLabel.text = [profile profileName];
     cell.selectionStyle = UITableViewCellSelectionStyleNone;
     return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [self.profileSelectionsTableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
    [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    ProfileSelection *profile = [self.profileSelections objectAtIndex:indexPath.row];
    self.mobileProfileId = [profile.profileId stringValue];
    [_continueButton setEnabled:YES];
}
dahiya_boy
  • 9,298
  • 1
  • 30
  • 51
user3453784
  • 593
  • 3
  • 9
  • 19

6 Answers6

1

How about this:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
  cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
koen
  • 5,383
  • 7
  • 50
  • 89
0

The selectionStyle property should be enough to remove the highlight.

If you want to show the checkmark after the used has selected the row, you need to implement the tableView:didSelectRowAtIndexPath: method

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.tableView cellForRowAtIndexPath:visibleIndexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
0

In Swift, the easiest way is to deselect a cell without animation right after it has been selected:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: false)
}
Fengson
  • 4,751
  • 8
  • 37
  • 62
  • This is more of a band-aid way of doing the objective. I would suggest setting the `cell.selectionStyle` to `.None` in `willDisplayCell` so the selection doesn't appear in the first place as Koen suggested. – jeffjv Sep 14 '16 at 23:48
0

delete your this function

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

Try to use this your problem will be solved

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];


    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone
    cell.textLabel.text = @"Checking Table View";
    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [tableView deselectRowAtIndexPath:indexPath animated:YES];


    }
Nischal Hada
  • 3,230
  • 3
  • 27
  • 57
0

Go to Storyboard. Select your Table View Cell. Set Selection to None. Then add below codes:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath   *)indexPath
{
     [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}
coolcool1994
  • 3,704
  • 4
  • 39
  • 43
0

This thread looks a bit old, but I actually came across a decent solution for this recently. In case anyone stumbles across this thread, I figure I might as well post an answer. Especially since I spent hours trying to track this down.

I can't find my original sources anymore, but here's what I found out:

The highlight is made by a background view on the UITableViewCell on the selectedBackgroundView property. If we have a subclass of the table cell, we can detect the transition to the editing state with willTransitionToState: and set the background view to one that is clear/white instead of the blue/gray styles.

Here's what I ended up doing, and it works pretty well.

- (void)willTransitionToState:(UITableViewCellStateMask)state {
    [super willTransitionToState:state];

    // To remove the blue/gray highlight on the table cell when selected,
    // set the selection background to a clear view.

    if (state & UITableViewCellStateShowingEditControlMask) {

        // If we're moving to an edit state, set the custom background view.

        UIView *bgView = [[UIView alloc] init];
        [bgView setTintColor:[UIColor whiteColor]];
        [bgView setBackgroundColor:[UIColor clearColor]];
        [self setSelectedBackgroundView:bgView];

    } else {

        // Otherwise, just remove it completely.
        // The system should handle the rest.

        [self setSelectedBackgroundView:nil];

    }
}

The plus side to this over the other solutions I've seen is that this one is quite minimal. No need for tracking instance variables or anything else.

ABeard89
  • 911
  • 9
  • 17