【发布时间】:2021-10-19 20:04:26
【问题描述】:
我的应用程序使用“过滤器”按钮,其中 whereField 查询根据按下的过滤器按钮进行细化。这是一个过滤之前的例子:
问题是,当我单击其中一个行时,它会将我带到与我的数据库中属于该行的原始 indexPath.row 对应的下一页。我怎样才能保留原来的indexPath.row?例如,单元格 B 始终为 indexPath.row = 1,即使在过滤之后也是如此。
这是我的第一个视图控制器的cellForRowAt。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Get a cell
let cell = tableView.dequeueReusableCell(withIdentifier: "MealPlanCell", for: indexPath) as! MealPlanCell
// Get the mealPlan that the tableView is asking about
let mealPlanInTable = mealPlan[indexPath.row]
// Customize the cell
cell.displayMealPlan(mealPlanInTable)
// Return the cell
return cell
}
以及如何在点击单元格后将此视图控制器的indexPath.row 连接到下一个视图控制器:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Detect the indexPath the user selected
let indexPath = tableView.indexPathForSelectedRow
// Get the mealPlan the user selected
let mealPlanSelected = mealPlan[indexPath!.row]
// Get a reference to the NextiewController
let NextVC = segue.destination as! NextViewController
// Get a reference to the currentMealPlanIndex in the NextViewController
NextVC.currentMealPlanIndex = indexPath!.row
}
非常感谢任何建议!
【问题讨论】:
-
所以我看到您的问题是您将索引传递给您的
NextVC,并且该索引应该对应于原始的未过滤数组,对吗?您是否有机会将mealPlan对象本身或可能是一个 id(稍后从数据库中查询该对象)传递给 NextVC? -
类似
NextVC.mealPlan = mealPlanSelected -
您应该将
indexPath.row视为表格视图控制器的私有实现细节。永远不要将它传递给另一个视图或视图控制器。你自己通过膳食计划。 -
一个叫“mealPlan”的人是从哪里来的?拥有一个为数据源中的每条记录显示唯一值的键。
标签: ios swift uitableview filter uistoryboardsegue