如何知道详细表视图的原始单元索引?

时间:2022-11-03 19:38:58

I implemented searchbar. It works okay when I only search for food. But When I try to edit the food from the search result, it only changes the search result array(foodSearching) but not for the original array(foods). So I need to know the indexpath for the original array(foods) when I edit the table view. Is there any help?

我searchbar实现。当我只搜索食物的时候,它是可以工作的。但是当我尝试从搜索结果中编辑食物时,它只更改搜索结果数组(food search),而不更改原始数组(food)。因此,当我编辑表格视图时,我需要知道原始数组(食物)的indexpath。有什么帮助吗?

selectedIndexPath is different for 'foodSearching' array (temporary array to show search result) and 'foods' array (original array to hold all of the data)

selectedIndexPath与“foodsearch”数组(显示搜索结果的临时数组)和“foods”数组(保存所有数据的原始数组)不同

@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()
    }
}

1 个解决方案

#1


1  

I would suggest not using indexPath as the reference to data since indexPath can change. If you were using Core Data, I would suggest objectID, but if you're not, I would suggest using any other uniquely identifiable information and use that as the reference. Creating following helper functions can help your implementation:

我建议不要使用indexPath作为对数据的引用,因为indexPath可以更改。如果您使用的是Core Data,我建议您使用objectID,但如果您不使用,我建议您使用其他唯一可识别的信息,并以此作为参考。创建以下帮助函数可以帮助您实现:

func indexPath(forFoodWithID uniqueID: IDType) -> NSIndexPath?
func foodID(forFoodAtIndexPath indexPath: NSIndexPath) -> IDType?

I hope that helps.

我希望有帮助。

#1


1  

I would suggest not using indexPath as the reference to data since indexPath can change. If you were using Core Data, I would suggest objectID, but if you're not, I would suggest using any other uniquely identifiable information and use that as the reference. Creating following helper functions can help your implementation:

我建议不要使用indexPath作为对数据的引用,因为indexPath可以更改。如果您使用的是Core Data,我建议您使用objectID,但如果您不使用,我建议您使用其他唯一可识别的信息,并以此作为参考。创建以下帮助函数可以帮助您实现:

func indexPath(forFoodWithID uniqueID: IDType) -> NSIndexPath?
func foodID(forFoodAtIndexPath indexPath: NSIndexPath) -> IDType?

I hope that helps.

我希望有帮助。