iPhone SDK - 在选择 UITableViewCell 时传递未显示在 UITableViewCell 中的数组中的数据
我已经搜索了所有教程或可以向我展示如何从填充单元格 cell.textlabel.text 字段的数组传递数据的内容,该字段在该节点中也有附加信息...
为了简单起见,我正在采取我正在从生成 XML 文件的 Web 服务中解析数据。它返回“ID”、“Name”、“OtherID”字段。然后用解析后的 XML 填充 UITableViewController,本质上只是将“Name”字段填充到 cell.textLabel.text 中。我已经完成了所有这些工作。
但是,我现在需要做的是:
当用户选择一行时,我需要将“ID”和“OtherID”字段(未在单元格中显示)设置为变量、切换视图并设置新的视图的 UILabels 以及旧视图中的变量。
这就是我不知所措的地方......
有人能给我指出如何完成此代码的教程或代码示例的方向吗?或者如果你有类似的东西可以分享给我吗?
非常感谢您抽出时间!
I have have searched all over for a tutorial or something that could show me how to pass data from an array that populates a cells cell.textlabel.text field that also has additional information in that node...
For simplicity sake I'm taking data I'm parsing from a Webservice that's producing an XML file. Which returns a "ID", "Name", "OtherID" field. Then populating a UITableViewController with the parsed XML essentially just the "Name" field to cell.textLabel.text. I have all of this working.
However, what I need to happen now is:
When a user does select a row I need the "ID" and "OtherID" field (that are not shown in the cell) to be set into variables, switch views, and set the new view's UILabels with the variables from the old view.
And this is where I'm at a loss...
Could someone point me in the direction of a tutorial or code example of how this code be done? Or if you have something similar share that with me?
Thank you so much for your time!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您在
tableView:cellForRowAtIndexPath:
中设置 UITableViewCell 时,您将根据单元格的索引路径使用结果数组中某个索引中的数据。执行您要求的操作的最简单方法是使用所选单元格的索引路径并执行与上面相同的计算以查找特定索引。然后你可以从原始数组中获取你需要的一切。看起来像这样:
稍微复杂一点(但可能更健壮)的方法是子类化 UITableViewCell 以添加属性或 ivars 来保存您需要的数据;这可以像单个属性一样简单来保存原始数组中的整个值。然后在选择方法中,您可以从单元格本身获取所需的所有内容。看起来像这样:
MyTableViewCell.h:
MyTableViewCell.m:
然后:
When you setup the UITableViewCell in
tableView:cellForRowAtIndexPath:
, you're using data from a certain index in the array of results, based on the index path of the cell.The easiest way to do what you ask is to use the index path for the selected cell and perform the same calculation as above to find that certain index. Then you can fetch everything you need from the original array. That would look something like this:
A slightly more complex (but possibly more robust) method would be to subclass UITableViewCell to add properties or ivars to hold the data you will need; this could be as simple as a single property to hold the entire value from the original array. Then in the selection method, you can fetch everything you need from the cell itself. That would look something like this:
MyTableViewCell.h:
MyTableViewCell.m:
And then: