2015-05-28 11 views
16

Im carico di una vista tabella da un file plist. Questo funziona senza problemi. Voglio semplicemente "spuntare" le righe a schede. Al momento con il mio codice non funziona come desiderato. Al momento loos in questo modo:Seleziona più righe in tableview e spunta quelle selezionate

  • rubinetto row1 (sarà tick riga 1 = buono)
  • rubinetto row1 di nuovo (non succede niente = male mi aspettano qui la fila per essere spuntata.) Mentre toccando nuovamente sulla riga 1 si spegne quindi. dopo che il secont tap su di esso.
  • Quando si tocca row0 al caricamento iniziale della Tableview non mi ha mai zecche fila

il mio codice:

class portals: UITableViewController { 

    var lastSelectedIndexPath = NSIndexPath(forRow: -1, inSection: 0) 

... 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! UITableViewCell 

     // Configure the cell... 
     cell.textLabel!.text = portals[indexPath.row] 

     return cell 
    } 


    // Check which portal is selected 
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

     var whichPortalIsSelected: String = "" 

     // Get Cell Label 
     let indexPath = tableView.indexPathForSelectedRow(); 

     // Tick the selected row 
     if indexPath!.row != lastSelectedIndexPath?.row { 

      let newCell = tableView.cellForRowAtIndexPath(indexPath!) 
      newCell?.accessoryType = .Checkmark 

      lastSelectedIndexPath = indexPath 

      whichPortalIsSelected = newCell!.textLabel!.text! 
      println("You selected cell #\(lastSelectedIndexPath.row)!") //PPP 
      println("You selected portal #\(whichPortalIsSelected)!") //PPP 

     // Un-Tick unselected row 
     } else { 
      let newCell = tableView.cellForRowAtIndexPath(indexPath!) 
      newCell?.accessoryType = .None 

      whichPortalIsSelected = newCell!.textLabel!.text! 
      println("You unselected cell #\(indexPath!.row)!") //PPP 
      println("You unselected portal #\(whichPortalIsSelected)!") //PPP 
     } 

    } 
} 

risposta

2

Ci sono molte soluzioni a questo problema, ecco uno mi si avvicinò con . Sto usando la proprietà "selected" della cella integrata, quindi la tableview la salva per noi. Assicurati solo nello storyboard o quando imposti il ​​tableview nel codice che utilizzi più selezioni.

import UIKit 

class TableViewController: UITableViewController 
{ 
    var lastSelectedIndexPath = NSIndexPath(forRow: -1, inSection: 0) 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
    { 
     let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! UITableViewCell 

     // Configure the cell... 
     cell.textLabel!.text = "row: \(indexPath.row)" 

     if cell.selected 
     { 
      cell.accessoryType = UITableViewCellAccessoryType.Checkmark 
     } 
     else 
     { 
      cell.accessoryType = UITableViewCellAccessoryType.None 
     } 

     return cell 
    } 

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 
    { 
     let cell = tableView.cellForRowAtIndexPath(indexPath) 

     if cell!.selected == true 
     { 
      cell!.accessoryType = UITableViewCellAccessoryType.Checkmark 
     } 
     else 
     { 
      cell!.accessoryType = UITableViewCellAccessoryType.None 
     } 
    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    { 
     return 100 
    } 
} 

Ho fatto un progetto di esempio qui: https://github.com/brcimo/SwiftTableViewMultipleSelection

+0

Questo non sembra funzionare. 'tableView: didSelectRowAtIndexPath:' viene chiamato solo quando la riga è selezionata, non quando è deselezionata, quindi 'cell.selected' sarà sempre vero! – entropid

6

Questo permette deselezionare.

class TableViewController: UITableViewController 
{ 
    var lastSelectedIndexPath = NSIndexPath(forRow: -1, inSection: 0) 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
    { 
     let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) 

     // Configure the cell... 
     cell.textLabel!.text = "row: \(indexPath.row)" 

     if cell.selected 
     { 
      cell.selected = false 
      if cell.accessoryType == UITableViewCellAccessoryType.None 
      { 
       cell.accessoryType = UITableViewCellAccessoryType.Checkmark 
      } 
      else 
      { 
       cell.accessoryType = UITableViewCellAccessoryType.None 
      } 
     } 

     return cell 
    } 

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 
    { 
     let cell = tableView.cellForRowAtIndexPath(indexPath) 

     if cell!.selected 
     { 
      cell!.selected = false 
      if cell!.accessoryType == UITableViewCellAccessoryType.None 
      { 
       cell!.accessoryType = UITableViewCellAccessoryType.Checkmark 
      } 
      else 
      { 
       cell!.accessoryType = UITableViewCellAccessoryType.None 
      } 
     } 
    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    { 
     return 100 
    } 
} 
+0

Questo è perfetto. Grazie. –

57

Swift 4

semplicemente sottoclasse UITableViewCell come questo:

class CheckableTableViewCell: UITableViewCell { 
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
     super.init(style: style, reuseIdentifier: reuseIdentifier) 
     self.selectionStyle = .none 
    } 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
    } 

    override func setSelected(_ selected: Bool, animated: Bool) { 
     super.setSelected(selected, animated: animated) 
     self.accessoryType = selected ? .checkmark : .none 
    } 
} 

Quindi utilizzare nel vostro cellForRowAt indexPath come tale:

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", 
    for: indexPath) as? CheckableTableViewCell 

Se è necessario, don dimenticarsi di sottoclasse tuo cellulare prototipo nel XI ter/storyboard:

enter image description here

+0

Questo modo è molto utile per favore dimmi come posso ottenere l'indice delle celle selezionate in vista tabella o meglio per guidarmi se ho una matrice di ulteriori informazioni sulle celle come posso avere una matrice di informazioni delle celle selezionate non tutte di essi –

+0

Sfortunatamente, quando si scorre un oggetto TableView, la proprietà 'isSelected' di una cella selezionata sembra essere persa quando si utilizzano celle di visualizzazione tabella riutilizzabili. Quindi puoi selezionare più celle, ottenere il segno di spunta, scorrere verso il basso nella tabellaVisualizza, scorrere di nuovo verso l'alto e le celle saranno deselezionate di nuovo. Qualche idea? – Alienbash

+0

Hai ragione @Alienbash, ho modificato la mia risposta di conseguenza. –

1

si deve fare una classe costume per ottenere lo stato selezionato della cella in cui è necessario ridefinire una func chiamato setSelected (_ selezionato: Bool, animato: Bool) o il segno di spunta verrà visualizzato in modo casuale mentre scorri ... Ecco un esempio di ciò che ho fatto: 1- creato un corso per la cella 2- aggiunto uno sbocco per un'immagine per visualizzare il segno di spunta (è possibile uscire da questo se non si desidera un'immagine di spunta costume) 3 - overrided la funzione e utilizzato il parametro selezionato: D

qui è la mia classe:

importazione UIKit

classe AddLocationCell: UITableViewCell {

@IBOutlet weak var check: UIImageView! 

override func awakeFromNib() { 
    super.awakeFromNib() 
    // Initialization code 
} 

override func setSelected(_ selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 
    if selected{ 
     check.image = UIImage(named:"check_active") 
    }else{ 
     check.image = UIImage(named:"check_normal") 

    } 
    // Configure the view for the selected state 
} 

}

4

Prima di tutto, vai al tuo Storyboard e seleziona si Tableview e nella finestra di ispezione Attributi, impostare Selection a selezione multipla.

Attributes Inspector with multiple selection

Poi, sovrascrivere il setSelected (_ selezionato: Bool, animato: Bool) funzione nella sottoclasse di UITableViewCell.

override func setSelected(_ selected: Bool, animated: Bool) { 
     super.setSelected(selected, animated: animated) 
     accessoryType = selected ? UITableViewCellAccessoryType.checkmark : UITableViewCellAccessoryType.none 
    } 
Problemi correlati