коли ви натискаєте на клітинку, рядок отримує виділення та виділення. Тепер я хочу відключити виділення, але дозволити виділення. Чи є спосіб обійти це. Є питання, яке відповідає на це, але воно вимикає як виділення, так і виділення.
Відповіді:
Ви можете просто встановити стиль вибору комірки на "Немає" з розкадрування:

Або з коду:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
Для Swift 3:
cell.selectionStyle = UITableViewCellSelectionStyle.none
Для Swift 4 і вище:
cell.selectionStyle = .none
Змінити UITableViewCell«S selectedBackgroundViewколір прозорий.
let clearView = UIView()
clearView.backgroundColor = UIColor.clearColor() // Whatever color you like
UITableViewCell.appearance().selectedBackgroundView = clearView
або встановити для певної комірки:
cell.backgroundView = clearView
cell.selectionStyle = UITableViewCellSelectionStyleNoneіноді щось глибоко розбиває логіку анімації Apple.
UITableViewCell.appearance().selectionStyle = .None
awakeFromNib()спрацьовує
стрімко 3
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
Щоб додати власний колір, використовуйте наведений нижче код. І зробити це прозорим використаннямalpha: 0.0
cell.selectedBackgroundView = UIView(frame: CGRect.zero)
cell.selectedBackgroundView?.backgroundColor = UIColor(red:0.27, green:0.71, blue:0.73, alpha:1.0)
Якщо ви використовуєте власний колір і хочете надати йому вигляд закругленого кута, використовуйте:
cell.layer.cornerRadius = 8
Крім того, використовуйте це для кращої анімації та відчуття
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
Для Objc:
[комірка setSelectionStyle: UITableViewCellSelectionStyleNone];
- (void)viewDidLoad {
[super viewDidLoad];
_tableView.allowsSelection = YES;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
.. .. .. ..
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
. . . . ..
}
Для Swift 5 найкращий спосіб:
cell.selectionStyle = .none
Ось рішення для swift 3, працює навіть у режимі редагування
cell.selectionStyle = .gray
cell.selectedBackgroundView = {
let colorView = UIView()
colorView.backgroundColor = UIColor.black.withAlphaComponent(0.0)
//change the alpha value or color to match with you UI/UX
return colorView
}()
cell.selectionStyle = UITableViewCellSelectionStyleNone;додайте це у свійcellForRowAtIndexPathспосіб