2017-06-29 3 views
0

Ich möchte, dass Benutzer maximal eine Stimme wählen können. Und dass das Häkchen zu dem Punkt springt, an dem Sie den anderen ausgewählt und abgewählt haben.Maximal ein Häkchen in TableView in Swift

Es sieht sehr einfach aus, aber ich sehe die Lösung nicht. Und ich kann die Antwort nicht im Internet finden.

Bitte kann mir jemand helfen?

Vielen Dank!

import UIKit 
import AVFoundation 

class voicesTableViewController: UITableViewController { 

    fileprivate let synthesizer = AVSpeechSynthesizer() 
    fileprivate var speechVoices = AVSpeechSynthesisVoice.speechVoices() 


    override func viewDidLoad() { 
     super.viewDidLoad() 

    } 


    // MARK: - Table view data source 

    override func numberOfSections(in tableView: UITableView) -> Int { 
     // #warning Incomplete implementation, return the number of sections 
     return 1 
    } 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     // #warning Incomplete implementation, return the number of rows 
     return speechVoices.count 
    } 


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 

     //Name 
     let voice = speechVoices[indexPath.row] 
     let voiceLang = voice.language as? String 
     let theVoice = UserDefaults.standard.object(forKey:"voice") as? String 

     cell.textLabel?.text = voice.name 


     // Language 
     if let language = countryName(countryCode: voice.language) { 
      cell.detailTextLabel?.text = "\(language)" 
     } 
     else { 
      cell.detailTextLabel?.text = "" 
     } 

     cell.detailTextLabel?.textColor = UIColor.gray 

     // Checkmark 

     if (theVoice != nil) { 
      if(theVoice == voiceLang) { 
       cell.accessoryType = UITableViewCellAccessoryType.checkmark 
      } 
     } 


     return cell 
    } 

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

     let voice = speechVoices[indexPath.row] 

     if tableView.cellForRow(at: indexPath)?.accessoryType == UITableViewCellAccessoryType.checkmark { 
      tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.none 
     } 
     else 
     { 
      //if ((tableView.indexPathsForSelectedRows?.count)! > 1) { 
       tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark 
      //} 
     } 

     UserDefaults.standard.set(voice.language, forKey:"voice") 
     UserDefaults.standard.synchronize() 


     tableView.deselectRow(at: indexPath, animated: true) 
    } 

    func countryName(countryCode: String) -> String? { 
     let preferredLanguage = NSLocale.preferredLanguages[0] as String 
     let current = Locale(identifier: preferredLanguage) 
     return current.localizedString(forLanguageCode: countryCode) ?? nil 

     //return current.localizedString(forIdentifier: indentifier) ? nil 
    } 


} 

Antwort

1

Einfache Funktionswechsel cellForRow:atIndexPath sollte funktionieren:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 

    //Name 
    let voice = speechVoices[indexPath.row] 
    let voiceLang = voice.language as? String 
    let theVoice = UserDefaults.standard.object(forKey:"voice") as? String 

    cell.textLabel?.text = voice.name 


    // Language 
    if let language = countryName(countryCode: voice.language) { 
     cell.detailTextLabel?.text = "\(language)" 
    } 
    else { 
     cell.detailTextLabel?.text = "" 
    } 

    cell.detailTextLabel?.textColor = UIColor.gray 

    // Checkmark 

    if(theVoice != nil && theVoice == voiceLang) { 
     cell.accessoryType = .checkmark 
    } else { 
     cell.accessoryType = .none 
    } 

    return cell 
} 

UPD # 1

Aber Sie bessere Lösung verwenden können: 1) Eigenschaft hinzufügen fileprivate var selectedIndexPath: IndexPath?

2) Änderungsfunktion func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) zum nächsten:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 

     //Name 
     let voice = speechVoices[indexPath.row] 
     let voiceLang = voice.language as? String 
     let theVoice = UserDefaults.standard.object(forKey:"voice") as? String 

     cell.textLabel?.text = voice.name 


     // Language 
     if let language = countryName(countryCode: voice.language) { 
      cell.detailTextLabel?.text = "\(language)" 
     } 
     else { 
      cell.detailTextLabel?.text = "" 
     } 

     cell.detailTextLabel?.textColor = UIColor.gray 

     // Checkmark 
     cell.accessoryType = self.selectedIndexPath == indexPath ? .checkmark : .none 
     return cell 
    } 

3) Und dann in func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) können Sie als nächstes tun:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

     let voice = speechVoices[indexPath.row] 

     self.selectedIndexPath = indexPath 
     UserDefaults.standard.set(voice.language, forKey:"voice") 
     UserDefaults.standard.synchronize() 


     tableView.deselectRow(at: indexPath, animated: true) 
     tableView.reloadRows(at: [indexPath], with: .automatic) 
    } 
+0

auch 'tableView.reloadData()' am Ende des 'didSelect'. – Slayter

+0

Ja, es funktioniert, danke! –