搜索期间未加载搜索表视图
Search Tableview not loading during Search
我的表格视图在搜索过程中没有重新加载。它正在调试并显示 venuesShown 实际上正在正确更新 - 计数准确,类型准确。
显示的场地属于 [场地] 类型 - 由名称和图像组成。
搜索时 - 如果不搜索 - 显示完整数组,否则按字母过滤并重新加载数据。
//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 { [=10=].venueName!.range(of: searchText, options: [.anchored, .caseInsensitive, .diacriticInsensitive]) != nil }
}
print("Venues Shown: \(venuesShown)")
self.chooseVenueTableView.reloadData()
self.chooseVenueTableView.layoutIfNeeded()
}
ViewDidLoad - 代表
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
}
行数
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchIsActive == false {
return venueArray.count
} else {
return venuesShown.count
}
}
Cell For Row - 搜索时切换到 venuesShown
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()
}
您在 searchIsActive == true 中错过了 return 个单元格 ...
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() }
你应该这样优化代码...
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
}
我的表格视图在搜索过程中没有重新加载。它正在调试并显示 venuesShown 实际上正在正确更新 - 计数准确,类型准确。
显示的场地属于 [场地] 类型 - 由名称和图像组成。
搜索时 - 如果不搜索 - 显示完整数组,否则按字母过滤并重新加载数据。
//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 { [=10=].venueName!.range(of: searchText, options: [.anchored, .caseInsensitive, .diacriticInsensitive]) != nil }
}
print("Venues Shown: \(venuesShown)")
self.chooseVenueTableView.reloadData()
self.chooseVenueTableView.layoutIfNeeded()
}
ViewDidLoad - 代表
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
}
行数
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchIsActive == false {
return venueArray.count
} else {
return venuesShown.count
}
}
Cell For Row - 搜索时切换到 venuesShown
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()
}
您在 searchIsActive == true 中错过了 return 个单元格 ...
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() }
你应该这样优化代码...
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
}