ios - 从 Tableview 更新 Viewcontroller 中的标签文本

标签 ios swift uitableview uiviewcontroller

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

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

经过一些研究后,我意识到这将在单元格本身的文本字段委托(delegate)的 textfieldDidEndEditing 函数中得到一些内容,但是我将如何从 View Controller 访问标签文本以便我可以更新它?我将提供下面的代码。

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

最佳答案

创建自定义委托(delegate),在 View Controller 中实现该委托(delegate)。将委托(delegate)传递给单元格,同时使单元格出队并在 textFieldDidEndEditing 中调用该委托(delegate),例如:

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
    }

}

关于ios - 从 Tableview 更新 Viewcontroller 中的标签文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51111536/

相关文章:

ios - 为每个按钮添加声音

iphone - 添加到可变数组的对象数量不正确

ios - 响应 UIViewController 中的表格单元格操作

ios - 如何使用swift2向 map View 中的第二个注释/pin添加不同的按钮操作

ios - NSMutableString 保留/复制是相同的(复制不起作用?)

swift - 是否对所有结构进行写时快速复制?

uitableview - NSDateFormatter

ios - 使多个 ScrollView 响应一个取消滚动行为

ios - OpenGL 纹理在使用 Swift 的 iOS 上不显示

swift - 无法滚动到可缩放图像中的每个位置