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:
And this is my TableView Controller:
Thanks in advance! :)
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! :)