从 Tableview 更新 Viewcontroller 中的标签文本

Posted

技术标签:

【中文标题】从 Tableview 更新 Viewcontroller 中的标签文本【英文标题】:Update label text in Viewcontroller from Tableview 【发布时间】:2018-06-30 03:40:30 【问题描述】:

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

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

在做了一些研究之后,我意识到这将在单元格本身的文本字段委托的 textfieldDidEndEditing 函数中得到一些,但是我将如何从视图控制器访问标签文本以便我可以更新它?我将在下面提供我的代码。

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】:

创建一个自定义委托,在您的视图控制器中实现该委托。将委托传递给单元,同时使单元出列并在 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
    


【讨论】:

我仍然可以在 textFieldDidEndEditing func 中添加数量和金额对吗?还是在我的视图控制器中实现自定义协议时这样做? 我也遇到了添加本身的问题。尝试使用 for in 循环会导致错误“Type 'String?”不符合协议'Sequence''。不太确定我做错了什么。 @优素福

以上是关于从 Tableview 更新 Viewcontroller 中的标签文本的主要内容,如果未能解决你的问题,请参考以下文章

如何从 BehaviorRelay observable 更新 tableView?

从 Tableview 更新 Viewcontroller 中的标签文本

从 CloudKit 检索新数据后更新 tableView 数据

从模型中自动更新 TableView 中的行

TableView 没有更新

从 tableview 中删除一行