search result not work went add tableviewcell

Multi tool use
Multi tool use


search result not work went add tableviewcell



Search bar is woking very well until I add custom tableview cell. It breaks when I search a keyword matching with a array. When I search wrong, it doesn't break.


var filteredArray = [String]()
var searchController = UISearchController()
var result = UITableViewController()

override func viewDidLoad() {
super.viewDidLoad()

searchController = UISearchController(searchResultsController: result)
tableView.tableHeaderView = searchController.searchBar
searchController.searchResultsUpdater = self

result.tableView.delegate = self
result.tableView.dataSource = self
result.tableView.register(UITableView.self, forCellReuseIdentifier: "Cell")

}

//config seachbar
func updateSearchResults(for searchController: UISearchController) {
filteredArray = array.filter( { (array : String) -> Bool in
if array.contains(searchController.searchBar.text!)
{
return true
}
else
{
return false
}

})
result.tableView.reloadData()
}



and this is a tableview cellForRow


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! SanPhamTableViewCell


// fill data into cell
if tableView == result.tableView
{
cell.name.text = filteredArray[indexPath.row]
cell.img.image = UIImage(named: ArrayImage[indexPath.row])

cell.price.text = ArrayPrice[indexPath.row]
cell.price.isUserInteractionEnabled = false

cell.desc.text = arrayDesc[indexPath.row]
cell.desc.isUserInteractionEnabled = false
}
else
{
cell.name.text = arrayName[indexPath.row]
cell.img.image = UIImage(named: arrayImage[indexPath.row])

cell.price.text = arrayPrice[indexPath.row]
cell.price.isUserInteractionEnabled = false

cell.desc.text = arrayDesc[indexPath.row]
cell.desc.isUserInteractionEnabled = false }

return cell
}



this is a error




2 Answers
2



You need to register the cell like this in viewDidLoad, replace:


viewDidLoad


result.tableView.register(UITableView.self, forCellReuseIdentifier: "Cell")



with:


result.tableView.register(SanPhamTableViewCell.self, forCellReuseIdentifier: "Cell")



//



Edit : for xib


result.tableView.register(UINib(nibName: "SanPhamTableViewCell", bundle: nil), forCellReuseIdentifier: Cell)





You can't register two different cell types for the same identifier.
– rmaddy
Jul 1 at 14:13





the other register is wrong this a replacement for it
– Sh_Khan
Jul 1 at 14:13





Sorry, thought you meant in addition to. Maybe clarify that is is a replacement, not an addition.
– rmaddy
Jul 1 at 14:15





i'd try but it have a new problem with cell.name.text = filteredArray[indexPath.row] "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value"
– Dung Tien
Jul 1 at 14:15






do you have xib cell or created it programmatically
– Sh_Khan
Jul 1 at 14:22



I am not sure how are you making the SanPhamTableViewCell, But if you are using a custom nib then that nib must also be registered with tableView


SanPhamTableViewCell


tableView


result.tableView.register(UINib(nibName: "SanPhamTableViewCell", bundle: nil), forCellWithReuseIdentifier: "Cell")





"Cannot invoke 'register' with an argument list of type '(UINib, forCellWithReuseIdentifier: String)" i try and it error like this
– Dung Tien
Jul 1 at 14:28





Are you setting up the identifier in the nib file for SanPhamTableViewCell ?
– ODev99
Jul 1 at 14:37






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

9,eoc7VzT,w
PJO7EBzWr r3E2aeIkESm7bl6Iiu4JJftP0uVPV ZKAxEhcDzs4Vu7XyXX

Popular posts from this blog

Rothschild family

Cinema of Italy