在IBAction中获取indexPath.row?
在我的应用程序中有一个表格视图。当选择表视图中的一行时,将出现 UIView
并显示信息。 行标题来自其中包含字符串的 plist 文件。 plist 文件还包含带有与行标题关联的电话号码的字符串。 在自定义 UIView 中,我有一个按钮,当您单击该按钮时,我希望它调用 plist 文件中声明的号码。 与用户单击的行关联的数字。
我怎样才能做到这一点?
该操作是一个普通的 IBAction:
- (IBAction)callNumber:(id)sender;
它连接到 IB 中的按钮(在 Xcode 4 中)。
如果有人能解决我的问题,我将不胜感激,谢谢。
编辑
为了清楚起见,我想获取与您在表格视图中选择的行关联的电话号码。 plist 中的字符串有一个键,用于电话号码名称:“phone”。
NSString *phone = [[tableList objectAtIndex:indexPath.row] objectForKey:@"phone"];
“tablelist”是一个 NSMutableArray。我想获取密钥“phone”并将其存储在 NSString *phone 中。所有这一切都在 IBAction 中。发布整个课程会有帮助吗?
In my app there is a table view. When a row in the table view is selected a UIView
appears and shows information.
The row title comes from a plist file with strings in it.
The plist file also includes strings with a phone number associated with the row title.
In the custom UIView I have a button, when you click the button I want it to call the number which is declared in the plist file.
The number which is associated with the row the user clicked.
How could I accomplish this?
The action is a ordinary IBAction:
- (IBAction)callNumber:(id)sender;
It is connected to the button in IB (in Xcode 4).
I would appreciate if someone had a solution to my problem, thanks.
Edit
For some clarity, I would like to get the phone number which is associated with the row you select in the table view. The string in the plist has a key, for the phone number name: "phone".
NSString *phone = [[tableList objectAtIndex:indexPath.row] objectForKey:@"phone"];
"tablelist" is a NSMutableArray. I would like to get the key "phone" and store it in the NSString *phone. All this in a IBAction. Would it help to post the whole class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有一个优雅的解决方案可以解决这个问题,那就是在设置 UITableViewCell 时。将 UIButton 的标签设置为行的索引:
然后,在操作代码中,您可以从标签中提取索引:
There is an elegant solution for this and it is when you setup the
UITableViewCell
. Set the tag of the UIButton to the index of the row:Then, in the action code you can pull the index out of the tag:
您需要在“自定义视图”上有一个属性来指示所选行的索引路径。您可以将其放入自定义视图控制器的标头中以声明此属性:
然后,像这样设置实现:
现在,在
tableView:didSelectRowAtIndexPath:
方法中,只需照常创建视图控制器,但设置其上的indexPath
属性,以便将来可以访问它:然后,在
MyCustomViewController.m
中的任何位置,只需编写self.indexPath.row
获取行所选的索引路径。You need to have a property on the "custom view" that indicates the index path of the row that was selected. You can put this in your custom view controller's header to declare this property:
Then, setup the implementation like this:
Now, in the
tableView:didSelectRowAtIndexPath:
method, simply create the view controller as usual, but set theindexPath
property on it so that it can be accessed in the future:Then, anywhere in
MyCustomViewController.m
, simply writeself.indexPath.row
to get the row of the index path that was selected.您可以采取以下快速临时解决方法:
单元格选择方法:
- 为选择的 indexPath.row 保存 NSUserDefault
在下一个视图中:
- 读取所读取的行的 NSUserDefault 值
这将使下一个视图知道选择了哪一行,并允许您获取其正确的数据。
What you could do as a quick temporary workaround is the following:
Cell Selected Method:
- save an NSUserDefault for what indexPath.row was selected
And in your next view:
- read the NSUserDefault value for whichever row was read
This will then let the next view know which one was selected and allow you to get the proper data for it.
如果 UITableViewCell 中有执行操作的控件,您需要知道索引路径,则可以使用以下几种方法:
如果您有单个部分(或带有控件的单元格的已知部分)并且没有使用
tag
属性进行其他操作,将行 + 1 存储在tag
属性中。然后,当调用该操作时,检索sender.tag - 1
并且您将获得行索引。从你的控件开始沿着超级视图链向上走,直到到达
UITableViewCell
。调用[tableView indexPathForCell:cell
来获取indexPath。创建控件的子类并将indexPath 存储在控件中。
If you have controls in a UITableViewCell that perform actions where you need to know the index path there are a few methods you can use:
if you have a single section (or a known section for the cells with controls) and are not using the
tag
property for something else, store the row + 1 in thetag
property. Then when the action is called, retrievesender.tag - 1
and you've got your row index.walk up the superview chain from your control until you get to the
UITableViewCell
. The call[tableView indexPathForCell:cell
to get the indexPath.create a subclass of your control and store the indexPath in the control.