Sobes.tech
Back to questions
Middle
103
questionbank

Как сделать так, чтобы при тапе на одну колонку другие не подсвечивались?

Sobes Copilot

Get answers in real time

Answer from AI

sobes.tech AI

Можно создать кастомный UITableViewCell и переопределить методы для управления состоянием выделения.

Вот пример:

import UIKit

class CustomCell: UITableViewCell {

    override func setSelected(_ selected: Bool, animated: Bool) {
        // Не вызываем super.setSelected
        // Управляем внешним видом самостоятельно, если это необходимо
    }

    override func setHighlighted изHighlightted: Bool, animated: Bool) {
        // Не вызываем super.setHighlighted
        // Управляем внешним видом самостоятельно, если это необходимо
    }
}

В tableView(_:cellForRowAt:) нужно использовать этот кастомный класс:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as? CustomCell else {
        fatalError("The dequeued cell is not an instance of CustomCell.")
    }
    // Настройка ячейки
    return cell
}

Также можно установить UITableViewCell.selectionStyle в .none:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifier", for: indexPath)
    cell.selectionStyle = .none // Отключает стандартную подсветку при тапе
    // Настройка ячейки
    return cell
}