传递“正确"从 UITableViewController 中的选定单元格到 ViewController 的数据 [英] Passing the "correct" Data from the selected cell in a UITableViewController to a ViewController
问题描述
我有一个 UITableviewController
,我在其中获取一个 JSON 文件并将其分解为各种对象以在我的 tableview
上显示(这很好用).
我还创建了变量来存储来自 tableview
中生成的单元格的某些信息,以便传递给 detailViewController
(文本和图像).
使用准备转场"方法,我能够将我需要的信息传递给detailViewController
.但是,我一直遇到一个问题,其中 detailViewController
中显示的数据不是所选单元格的数据,而是表格中最后一个加载的单元格显示的数据.>
我没有使用 didSelectRowAtIndexPath
因为它似乎并没有做得更好,而是创建了一个额外的 segue 屏幕(自动).
总而言之,我的 TableViewController
在屏幕上一次显示大约 4 个单元格.无论我选择哪个单元格,传递给 DetailViewController
的信息始终是屏幕上显示的第四个单元格的信息.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->UITableViewCell {let cell = tableView.dequeueReusableCellWithIdentifier("Post Cell", forIndexPath: indexPath) as!表视图单元格var dict = jsonArr[indexPath.row]cell.postTitle.text = dict["data"]!["post_title"] as?细绳cell.postTag.text = dict["data"]!["post_tags"] as?细绳cell.postAddress.text = dict["data"]!["post_address"] as?细绳titleToPass = cell.postTitle.textpostTagToPass = cell.postTag.textaddressToPass = cell.postAddress.text覆盖 func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {if (segue.identifier == "ListToDetail") {让 viewController = segue.destinationViewController as!DetailPostViewControllerviewController.passedTitle = titleToPassviewController.passedPostTags = postTagToPassviewController.passedAddress = addressToPass}}
cellForRowAtIndexPath
为每个单元格调用,因此您的变量将始终具有与要更新的最后一个单元格匹配的值是有意义的.
不要在该方法中设置要传递的数据,而是在 prepareForSegue
中调用 indexPathForSelectedRow:
并使用索引路径的行从 <代码>jsonArr.
I have a UITableviewController
where I take a JSON file and break it down into various objects for display on my tableview
(this works fine).
I've also created variables to store certain information from the cells generated in the tableview
to be passed on to a detailViewController
(text and images).
Using the 'prepare for segue' method I am able to pass the information I need to the detailViewController
. However, I keep running into an issue where the data displayed in the detailViewController
isn't the data of the cell selected but rather the data of what appears to the the last loaded cell on the table.
I am not using the didSelectRowAtIndexPath
because it doesn't seem to do much better but create an additional segue screen (automatically).
In summary, my TableViewController
displays about 4 cells at a time on the screen. regardless of which cell I select, the information passed to the DetailViewController
is always the information on the fourth cell displayed on the screen.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Post Cell", forIndexPath: indexPath) as! TableViewCell
var dict = jsonArr[indexPath.row]
cell.postTitle.text = dict["data"]!["post_title"] as? String
cell.postTag.text = dict["data"]!["post_tags"] as? String
cell.postAddress.text = dict["data"]!["post_address"] as? String
titleToPass = cell.postTitle.text
postTagToPass = cell.postTag.text
addressToPass = cell.postAddress.text
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "ListToDetail") {
let viewController = segue.destinationViewController as! DetailPostViewController
viewController.passedTitle = titleToPass
viewController.passedPostTags = postTagToPass
viewController.passedAddress = addressToPass
}
}
cellForRowAtIndexPath
gets called for every cell, so it makes sense that your variables will always have values matching the last cell to be updated.
Instead of setting the data you want to pass in that method, call indexPathForSelectedRow:
in prepareForSegue
and use the row of the index path to get the right things from jsonArr
.
这篇关于传递“正确"从 UITableViewController 中的选定单元格到 ViewController 的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!