xcode 如何通过触摸按钮获取 indexPath?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13014592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:53:53  来源:igfitidea点击:

How to get indexPath over touched button?

iosxcodeuitableview

提问by iWizard

I've got dynamic tableView and one problem. In each tableCell are two buttons. I'm trying to get indexPath.row on button touched but with no success.

我有动态 tableView 和一个问题。在每个 tableCell 中有两个按钮。我试图在触摸按钮上获取 indexPath.row 但没有成功。

My code:

我的代码:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

    NSIndexPath *indexPath = [self.homeTableView indexPathForSelectedRow];      

    NSLog(@"%d", indexPath.row);


}

How can I get indexPath.row for touced button (Segue connections are over this two buttons->two connections)?

我怎样才能为touced 按钮获取indexPath.row(Segue 连接在这两个按钮-> 两个连接上)?

回答by Tomasz Zab?ocki

It's not working because you do not select row when you click button.

它不起作用,因为您在单击按钮时没有选择行。

With two buttons I would disconnect your segues from buttons and do 2 manual segues in IB then add code like that to handle them:

使用两个按钮,我会将您的 segue 与按钮断开连接,并在 IB 中执行 2 个手动 segue,然后添加这样的代码来处理它们:

-(void)button1Pressed:(id)sender {
    UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
    NSIndexPath *clickedButtonIndexPath = [self.homeTableView indexPathForCell:clickedCell];
    ...
    [self performSegueWithIdentifier:@"YourManualSegueIdentifier1" sender:self];
}

Edit for second option:

编辑第二个选项:

In your MyTableViewCell.h:

在你的 MyTableViewCell.h 中:

@interface MyTableViewCell : UITableViewCell
    ...    
    @property (strong, nonatomic) NSIndexPath *cellIndexPath;
    ...
@end

In your MyTableViewCell.m:

在你的 MyTableViewCell.m 中:

-(void)button1Pressed:(id)sender {
    ...
    NSInteger row = cellIndexPath.row;
    ...
}

In your MyTableViewController.m:

在你的 MyTableViewController.m 中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];      
    }
    // remember index here!
    [cell setCellIndexPath:indexPath];
    ....
    return cell;
}

回答by Bhoopi

Apple change the UITableViewCell hierarchy in iOS 7

Apple 更改 iOS 7 中的 UITableViewCell 层次结构

Using iOS 6.1 SDK

使用 iOS 6.1 SDK

<UITableViewCell>
   | <UITableViewCellContentView>
   |    | <UILabel>

Using iOS 7 SDK

使用 iOS 7 SDK

<UITableViewCell>
   | <UITableViewCellScrollView>
   |    | <UITableViewCellContentView>
   |    |    | <UILabel>

So if you want to get indexpath at button index then use following code Using iOS 6 or later SDK

因此,如果您想在按钮索引处获取索引路径,请使用以下代码 Using iOS 6 or later SDK

UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
    NSIndexPath *clickedButtonPath = [book_table indexPathForCell:clickedCell];
    NSLog(@"index=%@",clickedButtonPath);

Using iOS 7 or 7+ SDK

使用 iOS 7 或 7+ SDK

 UITableViewCell *clickedCell = (UITableViewCell *)[[[sender superview] superview]superview];
    NSIndexPath *clickedButtonPath = [book_table indexPathForCell:clickedCell];
    NSLog(@"index=%ld",(long)clickedButtonPath.item);

回答by PJR

I am not using storyboard but once i did it in simple viewconroller based project, i think this may help you.

我没有使用故事板,但是一旦我在基于 viewconroller 的简单项目中使用它,我认为这可能会对您有所帮助。

 - (IBAction)goToNew:(id)sender 
{
   UIButton *btn = (UIButton*) sender;
   UITableViewCell *cell = (UITableViewCell*) [btn superview];
   NSIndexPath *indexPath = [tableView indexPathForCell:cell]; 
   int row = indexPath.row;
}

回答by Hanief

Maybe the touch happened to the UIButton instead of the UITableView / UITableViewCell.

也许触摸发生在 UIButton 而不是 UITableView / UITableViewCell。

Check with :

检查:

NSLog(@"%@",[sender class]);

回答by iWizard

Solved over button.tag

解决了 button.tag

In "cellForRowAtIndexPath" I set button.tag = indexPath.row and then in "- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {"

在“cellForRowAtIndexPath”我设置 button.tag = indexPath.row 然后在“- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {”

UIButton *btn = (UIButton*) sender;
NSLog(@"%d", btn.tag);

回答by Hemant

give button tag = indexpath.row+100;

给按钮 tag = indexpath.row+100;

and get..

并得到..

-(IBAction)ShowListView:(id)sender
{

    UIButton *btn = (UIButton *)sender;

    int Iindex = btn.tag-100;

    NSLog(@"button title is %@",btn.titleLabel.text);
    NSLog(@"button id is %d",Iindex);

    NSLog(@"array category is....%@",arrayCategory);