Sobes.tech
Middle

Ako zabezpečiť, aby pri klepnutí na jeden stĺpec sa ostatné nezvýrazňovali?

sobes.tech AI

Odpoveď od AI

Môžete vytvoriť vlastnú UITableViewCell a prepísať metódy na správu stavu výberu.

Tu je príklad:

import UIKit

class CustomCell: UITableViewCell {

    override func setSelected(_ selected: Bool, animated: Bool) {
        // Nevolajte super.setSelected
        // Spravujte vzhľad sami, ak je to potrebné
    }

    override func setHighlighted(_ highlighted: Bool, animated: Bool) {
        // Nevolajte super.setHighlighted
        // Spravujte vzhľad sami, ak je to potrebné
    }
}

V tableView(_:cellForRowAt:) by ste mali použiť túto vlastnú triedu:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as? CustomCell else {
        fatalError("Bunka v rade nie je inštanciou CustomCell.")
    }
    // Konfigurácia bunky
    return cell
}

Tiež môžete nastaviť UITableViewCell.selectionStyle na .none:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifier", for: indexPath)
    cell.selectionStyle = .none // Vypne štandardné zvýraznenie pri klepnutí
    // Konfigurácia bunky
    return cell
}