Sobes.tech
Middle

Jak zajistit, aby při klepnutí na jeden sloupec se ostatní nezvýrazňovaly?

sobes.tech AI

Odpověď od AI

Je možné vytvořit vlastní UITableViewCell a přepsat metody pro správu stavu výběru.

Zde je příklad:

import UIKit

class CustomCell: UITableViewCell {

    override func setSelected(_ selected: Bool, animated: Bool) {
        // Nevolat super.setSelected
        // Pokud je potřeba, spravujte vzhled sami
    }

    override func setHighlighted(_ highlighted: Bool, animated: Bool) {
        // Nevolat super.setHighlighted
        // Pokud je potřeba, spravujte vzhled sami
    }
}

V tableView(_:cellForRowAt:) je třeba použít tuto vlastní třídu:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as? CustomCell else {
        fatalError("Buňka ve frontě není instancí třídy CustomCell.")
    }
    // Nastavení buňky
    return cell
}

Také je možné nastavit UITableViewCell.selectionStyle na .none:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifier", for: indexPath)
    cell.selectionStyle = .none // Vypne výchozí zvýraznění při klepnutí
    // Nastavení buňky
    return cell
}