ios 如何在 UITableViewCell 中获取 UIButton 的 indexPath?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16229488/
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-08-30 23:21:00  来源:igfitidea点击:

How to get the indexPath of a UIButton in a UITableViewCell?

iosxcodeuitableview

提问by user2230971

I have a UITableviewwith multiple cells. On each cell I'm adding buttons dynamically according to some array count. So, when I click on a button I'm able to get the tagvalue of the button. But how to get the indexPathof that cell?

我有UITableview多个单元格。在每个单元格上,我根据一些数组计数动态添加按钮。所以,当我点击一个按钮时,我能够获得tag该按钮的值。但是如何获得indexPath那个单元格的?

Here is my code in -cellForRowAtIndexPath:

这是我的代码-cellForRowAtIndexPath

   UIView *view=(UIView*)[cell.contentView viewWithTag:indexPath.row+444];

   UIImageView *img=(UIImageView*)[cell.contentView viewWithTag:indexPath.row+999];
   img.image=[UIImage imageNamed:@"BHCS_empty.png"];

   if(integer!=50)
   {
        NSInteger y_axis=0;
        NSArray *Arr=[tableSubCategoryArr objectAtIndex:indexPath.row];
        img.image=[UIImage imageNamed:@"BHCS_selected.png"];

        view.Frame= CGRectMake(0,50,281, integer-50);
        for (int i=0; i<[Arr count]; i++)
        {
            NSLog(@"arr %@",[Arr objectAtIndex:i]);
            UIButton *Btn=[UIButton buttonWithType: UIButtonTypeCustom];
            Btn.frame=CGRectMake(0, y_axis, 281, 44);
            [Btn setImage:[UIImage imageNamed:@"BHCS_panel.png"] forState:UIControlStateNormal];
            [Btn addTarget:self action:@selector(subCategoryBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
            [Btn setTag:i+100];
            [view addSubview:Btn];


            UILabel *nameLbl=[[UILabel alloc]initWithFrame:CGRectMake(20, y_axis,248, 44)];
            nameLbl.text = [[Arr objectAtIndex:i]objectForKey:@"SubCategoryName"];
            nameLbl.textColor = [UIColor whiteColor];
            nameLbl.backgroundColor=[UIColor clearColor];
            panelTableView.separatorColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"BHCS_panel_div1.png"]];
            nameLbl.font = [UIFont fontWithName:@"Helvetica" size:12.0f];
            [view addSubview:nameLbl];

            y_axis=y_axis+44+1.3f;
        }
    }

采纳答案by user2230971

Thanks to all i slove this by using below code

感谢所有我通过使用下面的代码来解决这个问题

     NSIndexPath *indexPath = [panelTableView indexPathForCell:(UITableViewCell *)sender.superview.superview];

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

回答by Mrug

I have tried maximum of given answers, but at the and I generally use to go for most Generalised and ideal way as follows:

我已经尝试了最多给定的答案,但我通常使用以下最通用和理想的方法:

 CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
 NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];

回答by CW0007007

ALLLLL of the answers here are bad and you shouldn't be looping through superviews. Classic example, with iOS 7 Apple changed the tableViewCellhierarchy and your app will now crash!

这里的所有答案都很糟糕,你不应该在超级视图中循环。经典示例,在 iOS 7 中,Apple 更改了tableViewCell层次结构,您的应用现在会崩溃!

Use this instead:

改用这个:

How to know the UITableview row number

如何知道 UITableview 行号

回答by Amit

Updated answer

更新答案

Use it like:

像这样使用它:

CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.tableView]; 
NSIndexPath *hitIndex = [self.tableView indexPathForRowAtPoint:hitPoint];

回答by Balu

Put this code in the button's action method:

将此代码放在按钮的操作方法中:

NSIndexPath *indexPath = [yourtableviewname indexPathForCell:(UITableViewCell *)sender.superview];
   NSLog(@"%d",indexPath.row);

回答by cacodev

I suggest adding a property to your custom UITableViewCell implementation class to store the indexPath. Then, when your cellForRowAtIndexPath delegate fires in your TableViewController, set the indexPath property for that cell.

我建议向您的自定义 UITableViewCell 实现类添加一个属性来存储 indexPath。然后,当您的 cellForRowAtIndexPath 委托在您的 TableViewController 中触发时,为该单元格设置 indexPath 属性。

customTableViewCell.h :

customTableViewCell.h :

@interface customTableViewCell : UITableViewCell

@property NSIndexPath *indexPath;

@end

customTableViewController configure cell:

customTableViewController 配置单元格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    customTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"customTableViewCell" forIndexPath:indexPath];
    // Do whatever you want with your cell
    cell.indexPath = indexPath;

    return cell;
}

Then you can refer to the indexPath property in your customTableViewCell by calling self.indexPath

然后你可以通过调用 self.indexPath 来引用你的 customTableViewCell 中的 indexPath 属性

回答by Mike

None of these answers seem like a very clean solution. The way I would implement this is by using the delegate pattern. The view controller is the cell's delegate, meaning you let the cell itself handle the button press, and it tells its delegate when the button was pressed so it can handle it however it wants.

这些答案似乎都不是一个非常干净的解决方案。我实现这一点的方法是使用委托模式。视图控制器是单元格的委托,这意味着您让单元格本身处理按钮按下,并在按下按钮时通知其委托,以便它可以随心所欲地处理它。

Let's say you have a tableview where each cell represents a Personobject, and when the button is pressed you want to show a profile for this person. All you need to do is this:

假设您有一个表格视图,其中每个单元格代表一个Person对象,当按下按钮时,您想显示此人的个人资料。您需要做的就是:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PersonCell *cell = [tableView dequeueReusableCellWithIdentifier:@"customTableViewCell" forIndexPath:indexPath];
    cell.delegate = self;
    cell.person = self.people[indexPath.row];
    return cell;
}

- (void)personCell:(PersonCell *)personCell didPressButtonForPerson:(Person *)person {
    [self showProfileForPerson:person];
}

Then all you need to do in your button class is add a property called buttonPressedHandlerthat is a block passing back an instance of Person, and when you create your button and add the target do something like this:

然后,您需要在按钮类中添加一个名为的属性buttonPressedHandler,该属性是一个传递回 实例的块Person,当您创建按钮并添加目标时,请执行以下操作:

- (void)viewDidLoad {
    [super viewDidLoad];

    // do whatever whatever else you need/want to here

    [self.button addTarget:self selector:@selector(handleButtonPressed) forState:UIControlStateNormal];
}

- (void)handleButtonPressed {
    // Make sure this is a required delegate method, or you check that your delegate responds to this selector first...
    [self.delegate personCell:self didPressButtonForPerson:self.person];
}

回答by Fahri Azimov

You can use UITableView's indexPathForCell:method like so [tableView indexPathForCell:cell];. Good Luck!

您可以像这样使用UITableView'sindexPathForCell:方法[tableView indexPathForCell:cell];。祝你好运!