Удаление UITableViewCell Selection
В настоящее время я переопределяю стандартный UITableViewSelectionStyle, используя UITableViewSelectionStyleNone
, а затем меняя цвет ячейки на основе методов делегата:
- (void)tableView:(UITableView *)tableView
didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell setBackgroundColor:[UIColor yellowColor]];
}
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell setBackgroundColor:[UIColor whiteColor]];
}
- (void)tableView:(UITableView *)tableView
didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"indexpath: %i",indexPath.row);
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell setBackgroundColor:[UIColor whiteColor]];
}
- (void)tableView:(UITableView *)tableView
didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell setBackgroundColor:[UIColor whiteColor]];
}
Это почти работает, за исключением того, что всякий раз, когда я выделяю ячейку, а затем оттаскиваю палец от нее, не выбирая ее, цвет не меняется на белый... если я устанавливаю его в [UIColor RedColor], он работает с совершенством. Почему это...
Edit:
Как-то, когда я распечатываю indexPath.row после didUnhlightRowAtIndexPath, я получаю "indexpath: 2147483647" из моего NSLog
Ответы
Ответ 1
Вы можете попробовать:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
если вы хотите, чтобы подсветка исчезла после выбора ячейки. Если я не понял ваш вопрос.
Ответ 2
Вы также можете попробовать это
tableView.allowsSelection = NO;
другой способ сделать это
cell.selectionStyle = UITableViewCellSelectionStyleNone;
еще один
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Ответ 3
Вы также можете сделать это из раскадровки. Выберите tableViewCell и в разделе Attributes Inspector вы можете выбрать "Выбор" > "Нет"
![введите описание изображения здесь]()
Ответ 4
Вот версия Swift 2
if let indexPaths = self.tableView.indexPathsForSelectedRows {
for indexPath in indexPaths {
self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
}
Ответ 5
Посредством локального экземпляра моего indexPath я смог найти последнюю выбранную ячейку и изменить ее цвет на whitecolor. Кажется, действительно раздражает то, что я должен поддерживать само государство, но это то, что оно есть...
Ответ 6
Отменить выбор выбранных строк. Вам даже не нужно знать, какой это.
tableView.selectRow(at: nil, animated: true, scrollPosition: .none)
Ответ 7
Самое простое решение, которое сработало для меня (Swift 4.2)
(если вы все еще хотите, чтобы строка представления таблицы была выбираемой)
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let index = self.tableView.indexPathForSelectedRow{
self.tableView.deselectRow(at: index, animated: true)
}
}
Ответ 8
Как насчет этого с расширением?
import UIKit
extension UITableView {
func removeRowSelections() {
self.indexPathsForSelectedRows?.forEach {
self.deselectRow(at: $0, animated: true)
}
}
}
Использование:
tableView.removeRowSelections()