2017-06-16 38 views
0

我有2個部分,每16行,以及如何獲得tableview單元格中的所有文本字段值?當我點擊保存按鈕時想存儲它。當點擊保存按鈕並存儲在字典中時,在tableview單元格中獲取textfield?

我已經試着模擬Firebase數據庫中的數據放入String:AnyObject中,並在tableview中顯示它。

如何獲得文本字段或切換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 
    } 

我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 
    } 

} 

回答

0

你可以做財產以後這樣

@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 
    } 

附:在swift變量和函數中必須以小寫字符開頭,請參閱apple標準庫函數。

+0

我會嘗試一下,thx – benjamin

+0

這是可以在動態單元格中使用嗎?控制檯說:意外發現零,同時展開一個可選值 – benjamin

+0

對於遲到的反應感到抱歉,看來你必須投出單元格,我編輯答案 –

相關問題