2017-02-16 3 views
1

У меня есть UITableView внутри моего VC, и в основном то, что я хочу, это первая его часть, не подлежащая отображению. Но я не могу использовать isUserInteractionEnabled , потому что у меня есть UISwitch внутри каждой строки этого раздела. Установка selectionStyle на .none ничего не меняет. Я могу выбрать только No Selection в инспекторе интерфейса, чтобы отключить эти строки, но отключает всю таблицу. Что мне делать?UITableViewCell получает подсвечивание при нажатии

EDIT

Вот мой заказ сотовый класс

class CustomCell: UITableViewCell { override func setHighlighted(_ highlighted: Bool, animated: Bool) { if 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 } } }

+1

Проверьте: http://stackoverflow.com/questions/2267993/uitableview-how-to-disable-selection-for-some-rows-but-not-others – Priyal

ответ

3

Вы можете установить selectionStyle всех UITableViewCells в первом разделе .none следующим образом:

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 
} 

Тогда в вашем didSelectRowAtIndexPath() метод вы можете проверить 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 
    } 
} 
+0

Он по-прежнему подсвечивается при нажатии, как я уже сказал. Mb причина в том, что установка selectionStyle никому не позволяет выполнить didSelectRowAt, но он все еще вызывает willSelectRowAt –

+0

Я нашел причину, и это было довольно глупо с моей стороны. Я забыл, что я переопределил функцию setHighlighted в моем пользовательском подклассе UITableViewCell. Я хотел изменить цвет подключенной ячейки. Как мне это сделать вместо того, что я сделал? Ответ обновлен –

0

Вы должны установить стиль выбора на ячейку в коде.

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 
} 
0

в cellForRowAt добавить cell.selectionStyle = .none

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    if(indexPath.section == desiredSection){ 
     cell.selectionStyle = .none 
     return cell; 
    } 
0

Итак, я нашел причину, почему клетки с selectionStyle набором к .none получили выделенную на кране. Из-за меня перекрывая setHighlighted метод (как показано в этом вопросе) из UITableViewCell я добавил shouldHighlightRowAt метод как это:

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

Спасибо всем за помощь мне