部分中所选行的索引

时间:2018-05-24 17:22:31

标签: swift core-data

我有一个按部分分开的食物项目,我希望能够按下食物项目并转到食物项目详细信息页面。我的问题是我无法将信息传递给细节,因为我没有得到该部分中项目的索引。 FoodItem来自coredata。

 var allFoodItems = [[FoodItem]]()
 var foodItemTypes = [
FoodItemType.Appetizer.rawValue,
FoodItemType.SoupSalad.rawValue,
FoodItemType.Main.rawValue]

 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let foodItem = FoodItem()
    guard let section = foodItemTypes.index(of: foodItem.type!) else { return }

    let row = allFoodItems[section].count

    let selectedIndexPath = IndexPath(row: row, section: section)

    let foodItemDetailController = FoodItemDetailController()
    foodItemDetailController.foodItem = selectedIndexPath
    navigationController?.pushViewController(foodItemDetailController, animated: true)
}

enter image description here

1 个答案:

答案 0 :(得分:1)

您只需要直接发送indexPath

 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let foodItemDetailController = FoodItemDetailController()

    foodItemDetailController.foodItem = indexPath   // if  IndexPath is the type 

    navigationController?.pushViewController(foodItemDetailController, animated: true)
}

//

或发送点击的项目

 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let all = allFoodItems[indexPath.section]

    let item = all[indexPath.row]

    let foodItemDetailController = FoodItemDetailController()

    foodItemDetailController.foodItem = item // if foodItem is the type

    navigationController?.pushViewController(foodItemDetailController, animated: true)
}