UI Elements in TableViewCell are not working by touching, why?

Hey guys, built a tableView with Custom Cells and everything is looking good but when I want to use the elements in the Cells (Searchbar, SegmentControl) they are doing nothing, can someone help me please

For example, this is my SearchBar Cell:
Code Block
class SearchCell: UITableViewCell, UISearchBarDelegate {
    let searchbar = UISearchBar()
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        addSubview(searchbar)
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setConstraints() {
        searchbar.placeholder = "Country, City"
        searchbar.searchBarStyle = .minimal
        let TapGesture = UITapGestureRecognizer(target: self, action: #selector(searchBarTapped))
        searchbar.addGestureRecognizer(TapGesture)
        searchbar.isUserInteractionEnabled = true
        Constraints
        searchbar.translatesAutoresizingMaskIntoConstraints = false
        searchbar.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
        searchbar.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 10).isActive = true
        searchbar.widthAnchor.constraint(equalToConstant: 380).isActive = true
        searchbar.heightAnchor.constraint(equalToConstant: 40).isActive = true
    }
    @objc func searchBarTapped() {
        searchbar.delegate = self
    }
    override func awakeFromNib() {
        super.awakeFromNib() {
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
}


And this is my TableView Controller:

Code Block
class SearchViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {
    
    let cellClasses = SearchCell()
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
        return indexPath
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
                let cell = tableView.dequeueReusableCell(withIdentifier: "SearchCell", for: indexPath) as! SearchCell
        cell.setConstraints()
            cell.selectionStyle = .none
            return cell
        }
        if indexPath.row == 1 {
                let cell = tableView.dequeueReusableCell(withIdentifier: "SegmentControlCell", for: indexPath) as! SegmentControlCell
                cell.setUpSegmentControlCell()
            cell.selectionStyle = .none
                return cell
            } else {
                let basicCell = tableView.dequeueReusableCell(withIdentifier: "basicCell", for: indexPath) as! TestCell
                basicCell.setUpabel()
                basicCell.selectionStyle = .none
                return basicCell
        }
            
    }
    
    
    
    @IBAction func jumpToSearchViewController() {
        tabBarController?.selectedIndex = 2
    }
    let tableView = UITableView()
        
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.register(SearchCell.self, forCellReuseIdentifier: "SearchCell")
        tableView.register(SegmentControlCell.self, forCellReuseIdentifier: "SegmentControlCell")
        tableView.register(TestCell.self, forCellReuseIdentifier: "basicCell")
        tableView.dataSource = self
        tableView.delegate = self
        tableView.allowsSelection = true
        view.backgroundColor = .white
        navigationController?.navigationBar.prefersLargeTitles = true
        navigationItem.title = "Search"
        Functions
        configureTableView()
    }
  
    func configureTableView() {
        view.addSubview(tableView)
        tableView.rowHeight = 80
        Constraints
        tableView.translatesAutoresizingMaskIntoConstraints = false
        tableView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        tableView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        tableView.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
        tableView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
    }
        


Thanks in advance! :)
Answered by OOPer in 667187022
Your code is sort of broken (in SearchCell, line 32 is not a valid Swift statement and line 54 contains a dangling opening brace {),
so there may be other things to fix, but can you try changing line 9 to as follows?
Code Block
contentView.addSubview(searchbar)


Accepted Answer
Your code is sort of broken (in SearchCell, line 32 is not a valid Swift statement and line 54 contains a dangling opening brace {),
so there may be other things to fix, but can you try changing line 9 to as follows?
Code Block
contentView.addSubview(searchbar)


Thanks man! That's great, I used the wrong function but the first two points you mentioned are correct, I just tried to delete the spaces between the lines of code to make it shorter
This is not the code, it is something you have edited (line 109, 123, …)

So please copy the exact code.
And use Paste and match style to avoid all the extra lines, like this:
Code Block
class SearchCell: UITableViewCell, UISearchBarDelegate {
let searchbar = UISearchBar()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
addSubview(searchbar)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setConstraints() {
searchbar.placeholder = "Country, City"
searchbar.searchBarStyle = .minimal
let TapGesture = UITapGestureRecognizer(target: self, action: #selector(searchBarTapped))
searchbar.addGestureRecognizer(TapGesture)
searchbar.isUserInteractionEnabled = true
// Constraints
searchbar.translatesAutoresizingMaskIntoConstraints = false
searchbar.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
searchbar.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 10).isActive = true
searchbar.widthAnchor.constraint(equalToConstant: 380).isActive = true
searchbar.heightAnchor.constraint(equalToConstant: 40).isActive = true
}
@objc func searchBarTapped() {
searchbar.delegate = self
}
override func awakeFromNib() {
super.awakeFromNib() {
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}


Why do you set the delegate in searchBarTapped() ?
That's too late.
You should have done it earlier, probably in awake or in init.
UI Elements in TableViewCell are not working by touching, why?
 
 
Q