我的應用程序使用分頁,以顯示錶中的數據。在每5個食譜中,我用Alamofire發出一個新的請求來獲取數據並將其顯示在單元格中。我也有一個搜索欄,它使用相同的tableView來顯示搜索到的數據(也使用分頁)。
應用程序崩潰時,滾動和按搜索條上取消
當我按下取消按鈕時,將顯示來自服務器的最新食譜。
問題是如果我在兩個'頁'之間滾動
並按下取消按鈕,應用程序將崩潰。
我的猜測是,這是因爲應用程序會嘗試在與搜索結果單元格相對應的索引處填充單元格(這將高於第一個列表的結果)。
搜索結果中的配方與主列表中的配方相同,因爲它是模擬服務器(您可以查看控制檯以更好地瞭解正在進行的請求)。 視頻:https://www.youtube.com/watch?v=NGGZBZtY-J4&spfreload=10
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! RecipeTableViewCell
if searchController.isActive == false {
cell.loadRecipePreview(recipe: recipes[indexPath.row])
if indexPath.row == recipes.count - 1 {
recipeRequest()
}
}
else {
cell.loadRecipePreview(recipe: searchRecipes[indexPath.row])
if indexPath.row == searchRecipes.count - 1 {
recipeSearchRequest()
}
}
return cell
}
編輯:我認爲這個問題沒有什麼關係分頁,因爲這在兩個頁面之間進行滾動也會發生。
你在重新加載表之前或之後在哪裏設置「searchController.isActive」? –