Sobes.tech
Middle

Jak sprawić, aby przy dotknięciu jednej kolumny, inne się nie podświetlały?

sobes.tech AI

Odpowiedź od AI

Możesz utworzyć niestandardową komórkę UITableViewCell i nadpisać metody do zarządzania stanem zaznaczenia.

Oto przykład:

import UIKit

class CustomCell: UITableViewCell {

    override func setSelected(_ selected: Bool, animated: Bool) {
        // Nie wywołuj super.setSelected
        // Zarządzaj wyglądem samodzielnie, jeśli to konieczne
    }

    override func setHighlighted(_ highlighted: Bool, animated: Bool) {
        // Nie wywołuj super.setHighlighted
        // Zarządzaj wyglądem samodzielnie, jeśli to konieczne
    }
}

W tableView(_:cellForRowAt:) należy użyć tej niestandardowej klasy:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as? CustomCell else {
        fatalError("W kolejce nie ma instancji CustomCell.")
    }
    // Konfiguracja komórki
    return cell
}

Możesz również ustawić UITableViewCell.selectionStyle na .none:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifier", for: indexPath)
    cell.selectionStyle = .none // Wyłącza standardowe podświetlenie przy tapnięciu
    // Konfiguracja komórki
    return cell
}