20

I have a custom tableViewCell. I want to indicate user touch down by highlighting. The cell selection style is UITableViewCellSelectionStyleBlue. In the parent controller I have set self.clearsSelectionOnViewWillAppear = YES.

I should be good to go. Nope. Selection still sticks to the cell. What I want is selection indication only for the duration of the touch down. The appearance should immediately return to the unselected appearance on touch up.

How do I do this?

Cheers,
Doug

dugla
  • 12,774
  • 26
  • 88
  • 136

6 Answers6

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

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}
Dancreek
  • 9,524
  • 1
  • 31
  • 34
  • We have a winna! For the folks at home the actual method signature is deselectRowAtIndexPath:animated:. Sweet. Cheers. – dugla May 01 '12 at 19:36
  • I can't believe that I spend so much to figure out this pain problem... You just think cocoa touch will do that at didHighlightItemAtIndexPath any way thanks @Dancreek – andy shih Jan 04 '16 at 10:28
17

Below Swift example uses didHighlightRowAtIndexPath to change the background color of the cell on touch down and didUnhighlightRowAtIndexPath to reset the color - see below:

// MARK: UITableViewDelegate

func tableView(tableView: UITableView, didHighlightRowAtIndexPath indexPath: NSIndexPath) {
  if let cell = tableView.cellForRowAtIndexPath(indexPath) {
     cell.backgroundColor = UIColor.greenColor()
  }
}

func tableView(tableView: UITableView, didUnhighlightRowAtIndexPath indexPath: NSIndexPath) {
  if let cell = tableView.cellForRowAtIndexPath(indexPath) {
     cell.backgroundColor = UIColor.blackColor()
  }
}
CodeOverRide
  • 4,431
  • 43
  • 36
Zorayr
  • 23,770
  • 8
  • 136
  • 129
1

overwrite - (void)setSelected:(BOOL)selected animate:(BOOL)animated without calling super

Jonathan Cichon
  • 4,396
  • 16
  • 19
1

Update Zorayz answer. Swift 5

Set when setup tableView(ViewDidLoad)

tableView.allowsSelection = true

Add this method:

func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {
    if let cell = tableView.cellForRow(at: indexPath) as? SettingCell {
        cell.backView.backgroundColor = .red
    }
}
func tableView(_ tableView: UITableView, didUnhighlightRowAt indexPath: IndexPath) {
    if let cell = tableView.cellForRow(at: indexPath) as? SettingCell {
        cell.backView.backgroundColor = .green
    }
}

Custom cell with xib

class SettingCell: UITableViewCell {

    @IBOutlet weak var backView: UIView!
    @IBOutlet weak var imageIcon: UIImageView!
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var describeLabel: UILabel!
    

    override func awakeFromNib() {
        super.awakeFromNib()
        self.addSettings()
    }
    
    private func addSettings() {
        self.contentView.backgroundColor = .white
        self.backView.layer.cornerRadius = 12
        
        self.titleLabel.textColor = UIColor(hex: "#101010")
        self.describeLabel.textColor = UIColor(hex: "#676767")
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

    
    func setupCell(data: SettingModel) -> SettingCell {

        self.imageIcon.image = UIImage(named: data.iconImage)
        self.titleLabel.text = data.titleLabel
        self.describeLabel.text = data.describeLabel
        
        return self
    }
    
}
Evgeniy
  • 96
  • 1
  • 4
0

This works:

It gets the backgroundview with cell border looking like seperator.Do not change the default tableview settings in Interface builder.Make sure UITableViewCellSelectionStyleNone is NOT set to selectionstyle. I am pasting the working code. :

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *kCellIdentifier = @"PlayListCell";
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCellIdentifier];
}
MPMediaPlaylist *playList = [playlistCollection objectAtIndex:indexPath.row];
cell.textLabel.text = playList.name;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
 // cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d Songs",[playList.items count]];
MPMediaItemCollection *playListMediaCollection = [playlistCollection objectAtIndex:indexPath.row ];

cell.imageView.image =[UIImage imageWithCGImage:[self getImageForCollection:playListMediaCollection.items]];

// the main code which make it highlight

UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor colorWithRed:170.0f/255.0 green:170.0f/255.0 blue:170.0f/255.0 alpha:1.0f];
[bgColorView.layer setBorderColor:[UIColor blackColor].CGColor];
[bgColorView.layer setBorderWidth:1.0f];
[cell setSelectedBackgroundView:bgColorView];


return cell;

}

Ankish Jain
  • 11,305
  • 5
  • 36
  • 34
-4

I had the same issue.The code below perfectly worked for me.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
  • 2
    Do you really want to reload table on every touch event? It's absolutely wrong way how to do subject. – surfrider Aug 28 '14 at 08:04
  • This turns the cell blue, but it stays that way and it only works on touchUpInside. What about touch down only? – Ethan Parker Jan 16 '15 at 20:13
  • The cell is marked selected only after touch up - the question wants to change the color on touch down; plus, I agree with @surfrider, why would you reload data? – Zorayr May 14 '15 at 06:12