如何在表视图中快速访问视图控制器的值进入表视图

问题描述

我是新手,我无法访问tableview单元内tableview单元中的IBOutlet变量。

我的代码是这样的

在视图控制器中

@IBOutlet var lblPopupTitle: UILabel!

在单元格内的Tableview中

 cell.btnEdit.tag = indexPath.item
 cell.btnEdit.addTarget(self,action: #selector(btnEdit),for: .touchUpInside)

在“表格视图”单元格中

class AttendanceInOutCell: UITableViewCell {

@IBOutlet var txtStartAt: UITextField!

    override func awakeFromNib() {
        super.awakeFromNib()
        
    }
    
    
    @objc func btnEditClick(_ sender: UIButton)
    {
        let index = IndexPath(row: sender.tag,section: 0)
        let cell: AttendanceInsideCell = tableAway.cellForRow(at: index) as! AttendanceInsideCell

        lblPopupTitle.text = cell.txtAwayStart.text

    }
}

lblPopupTitle告诉我“使用未解析的标识符'lblPopupTitle'”如何解决此问题。 是否有人也遇到过这类问题。请帮忙!。 预先感谢!

解决方法

0

P.S:-如果需要,您还可以在委托函数中传递多个信息。

,
protocol AttendanceDelegate: class {
   func didTapOnBtn(_ popUpTitle: String)
}

class AttendanceInOutCell: UITableViewCell {

@IBOutlet var txtStartAt: UITextField!
    weak var delegate: AttendanceDelegate?
    override func awakeFromNib() {
        super.awakeFromNib()
        
    }
    
    
    @objc func btnEditClick(_ sender: UIButton)
    {
        let index = IndexPath(row: sender.tag,section: 0)
        let cell: AttendanceInsideCell = tableAway.cellForRow(at: index) as! AttendanceInsideCell //( I m not getting this from where you get this tableAway variable) // So I m just telling you how you can set lblpopupTitle.text in viewController

        lblPopupTitle.text = cell.txtAwayStart.text // remove this line
        self.delegate.didTapOnBtn(cell.txtAwayStart.text) // please unwrap the textfield text before passing it as parameter

    }
}



// for view controller 
// there must be as tableview cellforRowAt function where you are creating your cell
// so after creating the cell instance
// add this line ( cell.delegate = self) and return cell
// now add extension to your controller

extension ViewController: AttendanceDelegate {
   func didTapOnBtn(_ popUpTitle: String) {
       lblPopupTitle.text = popUpTitle
     
   }
}

// If you still face any problem you can ask me
Hope I am able to help you.
Thank You