【问题标题】:get textfield in tableview cell when tap Save button and store in dictionary?点击保存按钮并存储在字典中时获取表格视图单元格中的文本字段?
【发布时间】:2017-06-16 15:11:15
【问题描述】:

我有 2 个部分,每 16 行,如何获取 tableview 单元格中的所有文本字段值?当我点击保存按钮时想要存储它。

我已经从 Firebase 数据库中检索到 String:AnyObject 中的模拟数据,并将其显示在 tableview 上。

如何在 tableviewCell 中获取 textfield 或 switch 之类的值?

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
    }

}

【问题讨论】:

    标签: ios swift firebase tableview textfield


    【解决方案1】:

    你可以这样做

     @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中的变量和函数必须以小写字母开头,参见苹果标准库函数。

    【讨论】:

    • 这可以在动态单元格中使用吗?控制台说:在展开可选值时意外发现 nil
    • 抱歉回复晚了,看来你必须投单元格,我编辑了答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多