I have a tableView. I register the Cell as follow:
tableView.register(TVCellElementProperties.self, forCellReuseIdentifier: cellId1)
Inside TVCellElementProperties, I created manually a segment controller as follow:
let unitTypeSegmentedControl: UISegmentedControl = {
let types = ["Blue", "White"]
let sc = UISegmentedControl(items: types)
sc.selectedSegmentIndex = 0
sc.translatesAutoresizingMaskIntoConstraints = false
sc.tintColor = UIColor.darkBlue
sc.addTarget(self, action: #selector(handleUnitChange), for: .valueChanged)
return sc
}()
@objc func handleUnitChange() {
NotificationCenter.default.post(name: .unitPicked, object: self)
}
So, I think when I change the value inside the SegmentController, it should redirect me to the function handleUnitChange()
inside the tableView, I inserted this line into the ViewDidLoad:
NotificationCenter.default.addObserver(self, selector: #selector(unitPicked), name: .unitPicked, object: nil)
When I run the application, the function ** handleUnitChange** inside tableviewCell is not called. What I did wrong? how do I know what I clicked ?
EDIT: I am calling a setupView Function which responsible for insert the Segment Controller inside the Cell from init inside the UITableViewCell as follow:
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.setupViews()
print("test init")
}
So, when i run it, setupView is called just once, thus ** handleUnitChange** is called too just once. I mean to say, when the application is up and running, and when I click on the segment controller inside TableView, the function handleUnitChange is not called anymore.
I tried to call the function from the Cell inside CellForRow, but same as above. the function ** handleUnitChange** is not called overtime I clicked inside the Segment Controller.
if let cell = tableView.dequeueReusableCell(withIdentifier: cellId1, for: indexPath) as? TVCellElementProperties {
//cell.backgroundColor = UIColor.rgb(red: 12, green: 122, blue: 12)
//cell.contentView.isUserInteractionEnabled = false
cell.setupViews()
//print("\(cell.handleUnitChange(sender: u))")
cell.handleUnitChange(sender: cell.unitTypeSegmentedControl)
return cell
}