Sobes.tech
Middle

Come fare in modo che toccando una colonna, le altre non si evidenzino?

sobes.tech AI

Risposta dell'AI

È possibile creare una UITableViewCell personalizzata e sovrascrivere i metodi per gestire lo stato di selezione.

Ecco un esempio:

import UIKit

class CustomCell: UITableViewCell {

    override func setSelected(_ selected: Bool, animated: Bool) {
        // Non chiamare super.setSelected
        // Gestisci l'aspetto da solo, se necessario
    }

    override func setHighlighted(_ highlighted: Bool, animated: Bool) {
        // Non chiamare super.setHighlighted
        // Gestisci l'aspetto da solo, se necessario
    }
}

In tableView(_:cellForRowAt:) devi usare questa classe personalizzata:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as? CustomCell else {
        fatalError("La cella in coda non è un'istanza di CustomCell.")
    }
    // Configura la cella
    return cell
}

Puoi anche impostare UITableViewCell.selectionStyle su .none:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifier", for: indexPath)
    cell.selectionStyle = .none // Disattiva l'evidenziazione standard al tocco
    // Configura la cella
    return cell
}