从Tableview更新Viewcontroller中的标签文本

时间:2018-06-30 03:40:31

标签: ios swift uitableview uiviewcontroller

我的问题类似于这里的问题:Update label.text at runtime

我有一个ViewController,其中包含带有某些文本字段的tableview。当用户在透视图文本字段中输入数量和金额时,我想在运行时在最终键盘上按回车键后,在表视图外更新标签

经过研究后,我意识到这将在单元格本身的textfield委托的textfieldDidEndEditing函数中获得一些信息,但是我将如何从viewcontroller中访问标签文本,以便对其进行更新?我将在下面提供我的代码。

import UIKit
import RealmSwift

class MaterialsCell: UITableViewCell, UITextFieldDelegate{
    @IBOutlet weak var materialsDescription: UITextField!
    @IBOutlet weak var materialsQuantity: UITextField!
    @IBOutlet weak var materialsAmount: UITextField!

    func saveMaterialsData() {
        let saveMaterials = SPMaterialsRequest()
        saveMaterials.setValue(self.materialsDescription!.text, forKey: "materialDescriptiopn")
        saveMaterials.setValue(self.materialsQuantity!.text, forKey: "materialQuantity")
        saveMaterials.setValue(self.materialsAmount!.text, forKey: "materialAmount")
        let realm = try! Realm()

        do {
            try realm.write {
                realm.add(saveMaterials)
                print("added \(saveMaterials.materialDescription) to Realm Database")
                print("added \(saveMaterials.materialQuantity) to Realm Database")
                print("added \(saveMaterials.materialAmount) to Realm Database")
                print("added \(saveMaterials.materialTotal) to Realm Database")
            }
        } catch {
            print(error)
        }
    }

    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
    }

    func textFieldDidEndEditing(_ textField: UITextField) {

    }
}

import UIKit
import RealmSwift

class ServiceProMaterialsController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    @IBOutlet weak var materialsView: UITableView!
    @IBOutlet weak var materialsTotal: UILabel!

    var spMaterialsRequest: Results<SPMaterialsRequest>?

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellIdentifier = "MaterialsCell"

        guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? MaterialsCell
            else {
                fatalError("Dequed Cell is not an instance of MaterialsCell")
        }

        cell.materialsDescription.text = ""
        cell.materialsQuantity.text = ""
        cell.materialsAmount.text = "" 

        return cell
    }
}

1 个答案:

答案 0 :(得分:0)

创建一个自定义委托,在您的viewcontroller中实现该委托。将委托传递到单元格,同时使单元出队并在textFieldDidEndEditing中调用该委托,例如:

import UIKit
import RealmSwift

protocol CustomProtocol{
func textEntered(text : String,index:Int)
}

class MaterialsCell: UITableViewCell, UITextFieldDelegate{
    @IBOutlet weak var materialsDescription: UITextField!
    @IBOutlet weak var materialsQuantity: UITextField!
    @IBOutlet weak var materialsAmount: UITextField!

    var delegate : CustomProtocol?
    var index : Int?

    func saveMaterialsData() {
        let saveMaterials = SPMaterialsRequest()
        saveMaterials.setValue(self.materialsDescription!.text, forKey: "materialDescriptiopn")
        saveMaterials.setValue(self.materialsQuantity!.text, forKey: "materialQuantity")
        saveMaterials.setValue(self.materialsAmount!.text, forKey: "materialAmount")
        let realm = try! Realm()

        do {
            try realm.write {
                realm.add(saveMaterials)
                print("added \(saveMaterials.materialDescription) to Realm Database")
                print("added \(saveMaterials.materialQuantity) to Realm Database")
                print("added \(saveMaterials.materialAmount) to Realm Database")
                print("added \(saveMaterials.materialTotal) to Realm Database")
            }
        } catch {
            print(error)
        }
    }

    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
    }

    func textFieldDidEndEditing(_ textField: UITextField) {
        //Usage of custom protocol
        delegate?.textEntered(text : textField.text!,index:self.index!)
    }
}

import UIKit
import RealmSwift

class ServiceProMaterialsController: UIViewController, UITableViewDataSource, UITableViewDelegate , CustomProtocol{
    @IBOutlet weak var materialsView: UITableView!
    @IBOutlet weak var materialsTotal: UILabel!

    var spMaterialsRequest: Results<SPMaterialsRequest>?

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellIdentifier = "MaterialsCell"

        guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? MaterialsCell
            else {
                fatalError("Dequed Cell is not an instance of MaterialsCell")
        }
        //Do 1 and 2
        cell.delegate = self // 1
        cell.index = indexPath.row //2
        cell.materialsDescription.text = ""
        cell.materialsQuantity.text = ""
        cell.materialsAmount.text = "" 

        return cell
    }

   //Implementing CustomProtocol
   func textEntered(text : String,index:Int){
      materialsTotal.text = text
    }

}