Home > OS >  Search Tableview not loading during Search
Search Tableview not loading during Search

Time:09-17

My tableview is not reloading during search. It is debugging and showing that the venuesShown is in fact updating properly - count is accurate, the type is accurate.

Venues Shown is of type [Venue] - comprised of name and image.

While Searching - if not searching - shows full array otherwise filters by Letter and reloads data.

//MARK: Search Bar
var venuesShown : [VenueLookup] = []
var searchIsActive = false
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    if searchText.isEmpty {
        searchIsActive = false
    } else {
        searchIsActive = true
        self.venuesShown = venueArray.filter { $0.venueName!.range(of: searchText, options: [.anchored, .caseInsensitive, .diacriticInsensitive]) != nil }
    }
    print("Venues Shown: \(venuesShown)")
    self.chooseVenueTableView.reloadData()
    self.chooseVenueTableView.layoutIfNeeded()
}

ViewDidLoad - delegates

override func viewDidLoad() {
    super.viewDidLoad()
    venueNavBar.topItem?.title = college
    venueSearchBar.delegate = self
    getBars {
        self.chooseVenueTableView.reloadData()
    }
    chooseVenueTableView.delegate = self
    chooseVenueTableView.dataSource = self
    self.overrideUserInterfaceStyle = .light
}

Number of Rows

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if searchIsActive == false {
        return venueArray.count
    } else {
        return venuesShown.count   
    }
}

Cell For Row - switches to venuesShown if searching

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if venueArray.count > 0 && searchIsActive == false {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ChooseVenueTableViewCell", for: indexPath) as! ChooseVenueTableViewCell
        loadImageWith(imgView: cell.barImgView, url: venueArray[indexPath.row].venueImgNSString as String?)
        cell.venueLabel.text = venueArray[indexPath.row].venueName
            return cell
    } else if venueArray.count > 0 && searchIsActive == true {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ChooseVenueTableViewCell", for: indexPath) as! ChooseVenueTableViewCell
        loadImageWith(imgView: cell.barImgView, url: venuesShown[indexPath.row].venueImgNSString as String!)
        cell.venueLabel.text = venuesShown[indexPath.row].venueName!
        print(venuesShown[indexPath.row].venueName!)
    }
    return UITableviewcell()
}

CodePudding user response:

You have missed return cell in searchIsActive == true ...

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if venueArray.count > 0 && searchIsActive == false {
    let cell = tableView.dequeueReusableCell(withIdentifier: "ChooseVenueTableViewCell", for: indexPath) as! ChooseVenueTableViewCell
    loadImageWith(imgView: cell.barImgView, url: venueArray[indexPath.row].venueImgNSString as String?)
    cell.venueLabel.text = venueArray[indexPath.row].venueName
        return cell
} else if venueArray.count > 0 && searchIsActive == true {
    let cell = tableView.dequeueReusableCell(withIdentifier: "ChooseVenueTableViewCell", for: indexPath) as! ChooseVenueTableViewCell
    loadImageWith(imgView: cell.barImgView, url: venuesShown[indexPath.row].venueImgNSString as String!)
    cell.venueLabel.text = venuesShown[indexPath.row].venueName!
    print(venuesShown[indexPath.row].venueName!)
    return cell
}
return UITableviewcell() }

You should optimise code this way ...

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier:    "ChooseVenueTableViewCell", for: indexPath) as? ChooseVenueTableViewCell   ?? UITableviewcell()

let resultArray = searchIsActive ? venueArray : venuesShown

loadImageWith(imgView: cell.barImgView, url: resultArray[indexPath.row].venueImgNSString as String?)

cell.venueLabel.text = resultArray[indexPath.row].venueName

return cell 

}

   
  • Related