I have a table on which a cell
is made using .xib
. There are two view
s on this cell
. When I click on each view
, and I set the image and title for another viewController
. I made a protocol
for this, but I just can’t get this data on another viewController
because my delegate = nil
.
In this class, I set properties.
сlass DataTableViewCell: UITableViewCell {
var dataDelegete: DataAccountCellDelegete? = nil
@objc func accountViewTaped() {
dataDelegete?.navigationBarIcon(image: UIImage(named: "icon") ?? UIImage())
dataDelegete?.navigationBarTitle(title: "title")
}
@objc func settingsViewTaped() {
dataDelegete?.navigationBarIcon(image: UIImage(named: "icon") ?? UIImage())
dataDelegete?.navigationBarTitle(title: "title")
}
}
protocol DataAccountCellDelegete {
func navigationBarIcon(image: UIImage)
func navigationBarTitle(title: String)
}
In this class, I get and want to set these properties, but my delegate = nil
class AccountViewController: UIViewController {
var dataVC = DataTableViewCell()
override func viewDidLoad() {
super.viewDidLoad()
dataVC.delegete = self
}
}
extension AccountViewController: DataAccountCellDelegete{
func menuNavigationBarIcon(image: UIImage) {
menuNavigationBar.addImage(image)
}
func menuNavigationBarTitle(title: String) {
menuNavigationBar.addTitle(title)
}
}
How do I declare a delegate correctly?
The view hierarchy should be like
ViewController -> TableView -> TableViewCell
So, ViewController has the reference of TableView & TableView has the reference of the cell. So the data passing should be reversed.
TableViewCell -> TableView -> ViewController
as vadian said ,Cells are reused and being created in cellForRowAt. The variable dataVC is useless here.