[英]Swift search bar - updating table view with Alamofire request
我在數據庫上進行了一些字符串搜索,當用戶在搜索欄上按下第三個字符后,我返回了JSON,然后將所有結果附加到數組中,並用數據填充了表格視圖。
雖然我正在處理一個問題。 當我搜索說mens
我得到的結果出現在表格視圖中
如果我刪除所有文本並寫成女人,一切都很好...但是當我刪除一些字符串等時,我在表視圖cellForRowAt中得到了index out of range
的錯誤index out of range
。 我猜想它會中斷,因為tableview沒有時間獲取附加數組並填充單元格。
我試圖找出的是如何在reloadData()
查詢或reloadData()
設置時間延遲的情況下使其工作。 我不希望延遲時間的原因是,某些用戶的計算機可能運行緩慢-舊的iPhone,即使延遲2秒,也可能崩潰。
這是我的代碼
class SearchViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, UISearchControllerDelegate{
@IBOutlet weak var searchResultTable: UITableView!
let searchController = UISearchController(searchResultsController: nil)
var filteredData = [Products]()
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.isNavigationBarHidden = true
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
searchResultTable.tableHeaderView = searchController.searchBar
searchController.searchBar.delegate = self
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}
func getSearch(completed: @escaping DownloadComplete, searchString: String) {
if filteredData.isEmpty == false {
filteredData.removeAll()
}
let parameters: Parameters = [
"action" : "search",
"subaction" : "get",
"product_name" : searchString,
"limit" : "0,30"
]
Alamofire.request(baseurl, method: .get, parameters: parameters).responseJSON { (responseData) -> Void in
if((responseData.result.value) != nil) {
let result = responseData.result
if let dict = result.value as? Dictionary<String, AnyObject>{
if let list = dict["product_details"] as? [Dictionary<String, AnyObject>] {
for obj in list {
let manPerfumes = Products(productDict: obj)
self.filteredData.append(manPerfumes)
}
}
}
completed()
}
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView,
heightForRowAt indexPath: IndexPath) -> CGFloat {
return 170
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filteredData.count
}
func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:iPhoneTableViewCell = tableView.dequeueReusableCell(withIdentifier: "iPhoneTableCell", for: indexPath) as! iPhoneTableViewCell
let prods = self.filteredData[indexPath.row] //HERE I GET THE ERROR
cell.configureCell(products: prods)
return cell
}
}
extension SearchViewController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
if (searchController.searchBar.text?.characters.count)! >= 3 {
self.getSearch(completed: {
self.searchResultTable.reloadData()
self.searchResultTable.setContentOffset(CGPoint.zero, animated: true)
}, searchString: searchController.searchBar.text!)
} else {
self.searchResultTable.reloadData()
}
}
}
看起來您在更改tableView確實是reloadData
同時filteredData
的狀態。 如果numberOfRowsForSection
返回3,然后用戶按下了Delete鍵,則數組大小將變為0。這可能就是為什么cellForRow
在請求索引0、1或2時引發異常的原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.