在IBAction中获取indexPath.row?

发布于 2024-12-18 03:30:20 字数 686 浏览 6 评论 0原文

在我的应用程序中有一个表格视图。当选择表视图中的一行时,将出现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜味拾荒者 2024-12-25 03:30:20

有一个优雅的解决方案可以解决这个问题,那就是在设置 UITableViewCell 时。将 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;    
}

然后,在操作代码中,您可以从标签中提取索引:

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

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:

- (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
}
薄荷→糖丶微凉 2024-12-25 03:30:20

您需要在“自定义视图”上有一个属性来指示所选行的索引路径。您可以将其放入自定义视图控制器的标头中以声明此属性:

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

然后,像这样设置实现:

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

现在,在 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];
}

然后,在 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:

@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:

- (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.

黑凤梨 2024-12-25 03:30:20

您可以采取以下快速临时解决方法:

单元格选择方法:
- 为选择的 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.

长伴 2024-12-25 03:30:20

如果 UITableViewCell 中有执行操作的控件,您需要知道索引路径,则可以使用以下几种方法:

  1. 如果您有单个部分(或带有控件的单元格的已知部分)并且没有使用 tag 属性进行其他操作,将行 + 1 存储在 tag 属性中。然后,当调用该操作时,检索 sender.tag - 1 并且您将获得行索引。

  2. 从你的控件开始沿着超级视图链向上走,直到到达UITableViewCell。调用[tableView indexPathForCell:cell 来获取indexPath。

  3. 创建控件的子类并将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:

  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. Then when the action is called, retrieve sender.tag - 1 and you've got your row index.

  2. walk up the superview chain from your control until you get to the UITableViewCell. The call [tableView indexPathForCell:cell to get the indexPath.

  3. create a subclass of your control and store the indexPath in the control.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文