2
我實現了搜索欄。當我只搜索食物時它工作正常。但是當我嘗試從搜索結果中編輯食物時,它只會改變搜索結果數組(foodSearching),但不會改變原始數組(食物)。所以當我編輯表格視圖時,我需要知道原始數組(食物)的索引路徑。有什麼幫助嗎?如何知道詳細表格視圖的原始細胞索引?
selectedIndexPath
是「foodSearching」陣列(臨時數組顯示搜索結果)和「食品」陣列(原數組來保存所有的數據)
@IBAction func unwindToFoodList(sender: UIStoryboardSegue) {
if let sourceViewController = sender.sourceViewController as? FoodViewController, food = sourceViewController.food {
if let selectedIndexPath = tableView.indexPathForSelectedRow {
// Update an existing food.
if(isSearching){
foodSearching[selectedIndexPath.row] = food
// foodSearching array is temporary..
//need to find indexpath for foods array(original array)and update it as well...!!
tableView.reloadRowsAtIndexPaths([selectedIndexPath], withRowAnimation: .None)
}else{
foods[selectedIndexPath.row] = food
tableView.reloadRowsAtIndexPaths([selectedIndexPath], withRowAnimation: .None)
}
} else {
// Add a new food.
let newIndexPath = NSIndexPath(forRow: foods.count, inSection: 0)
foods.append(food)
tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Bottom)
}
// Save the foods.
saveFoods()
}
}
請勿編輯單元格。編輯你的數據模型,然後讓表重新加載。除非你的模型很大,否則重新加載整個表格。 – Gargoyle