2

I have a UITableView inside my VC, and basically what I want is the first section of it be non-tappable. But I cannot use isUserInteractionEnabled because I have UISwitch inside of each row of this section. Setting selectionStyle to .none changes nothing. I can only pick No Selection in the interface inspector to disable those rows, but it disables the entire table. What should I do?

EDIT

Here's my custom cell class

class CustomCell: UITableViewCell {
    override func setHighlighted(_ highlighted: Bool, animated: Bool) {

        if highlighted {
            self.backgroundColor = ColorConstants.onTapColor
        } else {
            self.backgroundColor = .clear
        }
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        if selected {
            self.backgroundColor = ColorConstants.onTapColor
        } else {
            self.backgroundColor = .clear
        }
    }

}

benc
  • 1,381
  • 5
  • 31
  • 39
Artem Misesin
  • 373
  • 3
  • 13
  • 1
    Check : http://stackoverflow.com/questions/2267993/uitableview-how-to-disable-selection-for-some-rows-but-not-others – Priyal Feb 16 '17 at 21:31

4 Answers4

4

You can set the selectionStyle of all UITableViewCells in the first section to .none as follows:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "YOURIDENTIFIER")

    if indexPath.section == 0 {
        cell.selectionStyle = .none
    } else {
        cell.selectionStyle = .default
    }

    return cell
}

Then in your didSelectRowAtIndexPath()method you can check if (indexPath.section != YOURSECTION):

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if indexPath.section == 0 {
        // DO NITHING
    } else {
        // DO WHATEVER YOU WANT TO DO WITH THE CELLS IN YOUR OTHER SECTIONS
    }
}
Stefan Stefanov
  • 829
  • 7
  • 23
  • It still gets highlighted on tap, as I said. Mb the reason for that is that setting selectionStyle to none avoids executing didSelectRowAt, but it still calls willSelectRowAt – Artem Misesin Feb 17 '17 at 02:09
  • I found the reason and it was pretty silly of my part. I forgot, that I overrided setHighlighted function in my custom UITableViewCell subclass. I wanted to change color of tapped cell. How should I do it instead of what I did? Answer updated – Artem Misesin Feb 17 '17 at 02:16
1

So, I found the reason why cells with selectionStyle set to .none got highlighted on tap. Because of me overriding setHighlighted method (as shown in the question) of UITableViewCell I added shouldHighlightRowAt method like this:

func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
    if indexPath.section == 0 {
        return false
    } else {
        return true
    }
}

Thanks everybody for helping me

Artem Misesin
  • 373
  • 3
  • 13
0

You will have to set the selection style per cell in code.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = table.dequeue...

    if indexPath.section == 0 {
        cell.selectionStyle = .none
    } else {
        cell.selectionStyle = .default
    }

    return cell
}
keithbhunter
  • 12,258
  • 4
  • 33
  • 58
0

in cellForRowAt add cell.selectionStyle = .none

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
     if(indexPath.section == desiredSection){
        cell.selectionStyle = .none
        return cell;
     }
Faruk Duric
  • 794
  • 1
  • 7
  • 10