2017-06-16 1 views
0

J'ai 2 section avec chaque 16 lignes, et comment obtenir toute la valeur de champ de texte dans la cellule de tableview? vouloir le stocker quand je tape sur le bouton de sauvegarde.Afficher le champ de texte dans une cellule de tablevue lorsque vous appuyez sur le bouton Enregistrer et que vous le stockez dans un dictionnaire?

et je reprends déjà simuler des données à partir de la base de données Firebase mis en String: AnyObject, et l'afficher sur tableview.

comment obtenir de la valeur comme textfield ou basculer dans tableviewCell?

import UIKit 
import Firebase 
import FirebaseDatabaseUI 



class SettingLabelTableViewController: UITableViewController, UITextFieldDelegate { 

    var BitArray:[String] = ["M0","M1","M2","M3","M4","M5","M6","M0","M8" 
     ,"M9","M10","M11","M12","M13","M14","M15"] 
    var WordArray:[String] = ["D0","D1","D2","D3","D4","D5","D6","D0","D8" 
     ,"D9","D10","D11","D12","D13","D14","D15"] 
    var DeviceKey:String? 
    var Ref: FIRDatabaseReference! 
    var dict = [String:AnyObject]() 
    var allCellsText = [String]() 
    @IBAction func SaveButton(_ sender: UIBarButtonItem) 
    { 


     /*self.Ref.setValue(dict, withCompletionBlock: 
     { (error, dbref) in 

     })*/ 
    } 

    override func viewDidLoad() 
    { 
     super.viewDidLoad() 
     Ref = device.child("DEVICE").child(DeviceKey!).child("SETTINGS").child("LABEL") 
    } 


    override func viewWillAppear(_ animated: Bool) { 
     super.viewWillAppear(true) 
     Ref.observeSingleEvent(of: .value, with: { (snapshot) in 
      if !snapshot.exists(){ 
       print("not exist") 
       csGolbal.g_NameAry.removeAll() 
       self.dict.removeAll() 
      } 
      else{ 
       self.dict.removeAll() 
       self.dict = (snapshot.value as? [String: AnyObject])! 


       /*for item in snapshot.value as! [String : String]{ 
        csGolbal.g_NameAry.append([item.key:item.value]) 
       }*/ 
      } 
      self.tableView.reloadData() 
     }) 
    } 


    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 

    override func numberOfSections(in tableView: UITableView) -> Int 
    { 
     return 2 
    } 

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

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

     cell.LabelTitle.text = BitArray[indexPath.row] 
     if dict.count > 0{ 
      cell.txtName.text = dict["M"+String(indexPath.row)] as? String ?? "null" 

     } 
    return cell 
    } 

mon tableviewCell

class LabelNameTableViewCell: UITableViewCell 
{ 
    @IBOutlet weak var LabelTitle: UILabel! 
    @IBOutlet weak var txtName: UITextField! 

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

    override func setSelected(_ selected: Bool, animated: Bool) { 
     super.setSelected(selected, animated: animated) 

     // Configure the view for the selected state 
    } 

} 

Répondre

0

Vous pouvez faire somthing comme ça

@IBAction func saveButton(_ sender: UIBarButtonItem) { 
     var dict: [String:String] = [:] 
     for (i,bit) in bitArray.enumarate() { 
      let cell = tableView.cellForRow(at: IndexPath(row: i, section: 0)) as! LabelNameTableViewCell 
      dict[bit] = cell.txtName.text 
     } 
     for (i,word) in wordArray.enumarate() { 
      let cell = tableView.cellForRow(at: IndexPath(row: i, section: 1)) as! LabelNameTableViewCell 
      dict[word] = cell.txtName.text 
     } 
     // dict ---- "M0": "some text" ... 
     //then store it 
    } 

P.S. Dans les variables swift et la fonction doit commencer par un caractère en minuscule, voir les fonctions de la bibliothèque standard Apple.

+0

Je vais essayer, thx – benjamin

+0

est-ce que cela peut utiliser dans une cellule dynamique? console dit: inattendue trouvé nil tout en déballant une valeur facultative – benjamin

+0

désolé pour la réponse tardive, il semble que vous devez jeter la cellule, j'ai édité la réponse –