简体   繁体   English

在IBAction中获取indexPath.row?

[英]Get indexPath.row in IBAction?

In my app there is a table view. 在我的应用程序中有一个表格视图。 When a row in the table view is selected a UIView appears and shows information. 在表格视图中选择一行时,将显示一个UIView并显示信息。 The row title comes from a plist file with strings in it. 行标题来自带有字符串的plist文件。 The plist file also includes strings with a phone number associated with the row title. plist文件还包括带有与行标题关联的电话号码的字符串。 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. 在自定义UIView中,我有一个按钮,当您单击该按钮时,我希望它调用在plist文件中声明的号码。 The number which is associated with the row the user clicked. 与用户单击的行关联的数字。

How could I accomplish this? 我该怎么做?

The action is a ordinary IBAction: 该动作是普通的IBAction:

- (IBAction)callNumber:(id)sender;

It is connected to the button in IB (in Xcode 4). 它连接到IB中的按钮(在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". plist中的字符串具有电话号码名称的关键字:“ phone”。

NSString *phone = [[tableList objectAtIndex:indexPath.row] objectForKey:@"phone"];

"tablelist" is a NSMutableArray. “表列表”是NSMutableArray。 I would like to get the key "phone" and store it in the NSString *phone. 我想获取密钥“ phone”并将其存储在NSString * phone中。 All this in a IBAction. 所有这些都在IBAction中。 Would it help to post the whole class? 全班上课有帮助吗?

There is an elegant solution for this and it is when you setup the UITableViewCell . 有一个很好的解决方案,那就是在设置UITableViewCell Set the tag of the UIButton to the index of the row: 将UIButton的标签设置为行的索引:

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString* CellIdentifier = @"Cell";

    YourCustomCell* cell = (YourCustomCell*)[tableView 
                              dequeueReusableCellWithIdentifier:CellIdentifier];

    //  All the code for loading a cell

    //  Other code you have for configuring the cell

    //  HERE'S THE IMPORTANT PART: SETTING THE
    //  BUTTON'S TAG TO THE INDEX OF THE ROW
    cell.phoneButton.tag = indexPath.row;

    return cell;    
}

Then, in the action code you can pull the index out of the tag: 然后,在操作代码中,您可以从标签中拉出索引:

- (IBAction)callNumber:(id)sender {
    UIButton* button = sender;
    int index = button.tag;
    NSString *phone = [[tableList objectAtIndex:indexPath.row] objectForKey:@"phone"];
    //  Make the phone call
}

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: 您可以将其放在自定义视图控制器的标头中以声明此属性:

@interface MyCustomViewController : UIViewController {
    ...
    NSIndexPath * indexPath;
}
@property (nonatomic, retain) NSIndexPath * indexPath;

Then, setup the implementation like this: 然后,像这样设置实现:

@implementation MyCustomViewController
@synthesize indexPath;
...
// only needed if not using ARC
- (void)dealloc {
    self.indexPath = nil;
    ...
    [super dealloc];
}
@end

Now, in the tableView:didSelectRowAtIndexPath: method, simply create the view controller as usual, but set the indexPath property on it so that it can be accessed in the future: 现在,在tableView:didSelectRowAtIndexPath:方法中,只需照常创建视图控制器,但是在其上设置indexPath属性,以便将来可以访问它:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MyCustomViewController * vc = [[MyCustomViewController alloc] initWithBundle:nil nibName:nil];
    [vc setIndexPath:indexPath];
    [self presentModalViewController:vc];
    // if not using ARC
    [vc release];
}

Then, anywhere in MyCustomViewController.m , simply write self.indexPath.row to get the row of the index path that was selected. 然后,在MyCustomViewController.m任何位置,只需编写self.indexPath.row即可获取所选索引路径的行。

What you could do as a quick temporary workaround is the following: 作为快速的临时解决方法,您可以执行以下操作:

Cell Selected Method: - save an NSUserDefault for what indexPath.row was selected 单元格选定的方法:-为选择的indexPath.row保存一个NSUserDefault。

And in your next view: - read the NSUserDefault value for whichever row was read 在下一个视图中:-读取读取的任何行的NSUserDefault值

This will then let the next view know which one was selected and allow you to get the proper data for it. 然后,这将使下一视图知道选择了哪个视图,并允许您为其获取正确的数据。

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: 如果您在UITableViewCell中具有控件,这些控件可以执行需要了解索引路径的操作,则可以使用以下几种方法:

  1. 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 the tag property. 如果您只有一个部分(或带有控件的单元格的已知部分),并且没有将tag属性用于其他用途,请将行+ 1存储在tag属性中。 Then when the action is called, retrieve sender.tag - 1 and you've got your row index. 然后,当调用该操作时,检索sender.tag - 1 ,您便拥有了行索引。

  2. walk up the superview chain from your control until you get to the UITableViewCell . 从您的控件走到超级视图链,直到到达UITableViewCell为止。 The call [tableView indexPathForCell:cell to get the indexPath. 调用[tableView indexPathForCell:cell以获取indexPath。

  3. create a subclass of your control and store the indexPath in the control. 创建控件的子类,并将indexPath存储在控件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM