I have a table and a custom cell. I get the values from the table and pass them to the cell. From the cell, I have to pass these values to the VC. But if there are these values in the cells, then on VC they are already = nil
class CloseMonthTableViewCell : UITableViewCell {
var delegate: StatusCloseMonthDelegate?
@IBAction func setStatus(_ sender: Any) {
self.delegate?.setStatus(status: 2, empId: empId ?? 0)
}
}
protocol StatusCloseMonthDelegate: class {
func setStatus(status: Int, empId: Int)
}
DialocVC
class ApproveDialogView: UIViewController {
var cellDelegate = CloseMonthTableViewCell()
var status: Int?
var empId: Int?
override func viewDidLoad() {
super.viewDidLoad()
cellDelegate.delegate = self
}
@IBAction func setStatus(_ sender: Any) {
print(status,empId) // nil and nill Why are they = nil?
}
}
extension ApproveDialogView: StatusCloseMonthDelegate {
func setStatus(status: Int, empId: Int) {
self.status = status
self.empId = empId
}
}
Do a little more reading on how to use protocol / delegate pattern.
class ApproveDialogView: UIViewController {
// get rid of this line!
//var cellDelegate = CloseMonthTableViewCell()
var status: Int?
var empId: Int?
override func viewDidLoad() {
super.viewDidLoad()
// get rid of this line!
//cellDelegate.delegate = self
}
@IBAction func setStatus(_ sender: Any) {
print(status,empId) // nil and nill Why are they = nil?
}
}
I'm assuming you also have an extension for UITableViewDataSource
and UITableViewDelegate
:
extension ApproveDialogView: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CloseMonthTableViewCell
// normal cell settings
// set the cell's delegate here!
cell.delegate = self
return cell
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.