Reputation: 73
I have a Food Items separated by sections and I want to be able to press on the food item and go to a food item detail page. My problem is that I am not able to pass the information to the detail because I am not getting the index of the item in the section. FoodItem is from 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)
}
Upvotes: 0
Views: 65
Reputation: 100503
You only need to send indexPath directly
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let foodItemDetailController = FoodItemDetailController()
foodItemDetailController.foodItem = indexPath // if IndexPath is the type
navigationController?.pushViewController(foodItemDetailController, animated: true)
}
//
Or send the item clicked
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)
}
Upvotes: 1