如何通过iOS中的参数将UITableView IndexPath传递给UIButton @selector? [英] How to pass UITableView IndexPath to UIButton @selector by parameters in iOS?

查看:107
本文介绍了如何通过iOS中的参数将UITableView IndexPath传递给UIButton @selector?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UITableViewCells 中添加了 UIButton 。我有用户单击按钮时,我们已获得索引路径以使用 NSMutableArray 中的值。我使用下面的方法来获取当前的 IndexPath

I have added UIButton in UITableViewCells. I have when the user clicks the button we have get the indexpath to use the values from NSMutableArray. I have used the below to get the current IndexPath,

[getMeButton addTarget:self action:@selector(resendTheErrorMessage:) forControlEvents:UIControlEventTouchUpInside];

-(void) resendTheErrorMessage:(id)sender 
{
   NSLog(@"SEnder: %@", sender);
   //NSLog(@"Index Path : %@", indexpath);
}

任何人都可以帮我传递当前的indexpath UIButton's @selector。提前致谢。

Can anyone please help me to pass current indexpath UIButton's @selector. Thanks in advance.

编辑:

这是我从<$ c $获得的输出c> NSLog()

<UIButton: 0x864d420; frame = (225 31; 95 16); opaque = NO; tag = 105; layer = <CALayer: 0x864d570>>


推荐答案

添加 UIButton 喜欢这个

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(10.0, 2.0, 140.0, 40.0)];
[btn setTitle:@"ButtonTitle" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[btn setTag:indexPath.row];
[cell.contentView addSubview:btn];

然后获取其标签号 -

And then get its tag number -

-(void)buttonClicked:(id)sender
{
    NSLog(@"tag number is = %d",[sender tag]);
    //In this case the tag number of button will be same as your cellIndex.
   // You can make your cell from this.

   NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[sender tag] inSection:0];
   UITableViewCell *cell = [tblView cellForRowAtIndexPath:indexPath];
}

注意: 以上当tableView只有1个部分时,解决方案将起作用。如果您的tableView有多个部分,您应该知道您的部分索引或者使用以下方法。

Note: Above solution will work when your tableView has only 1 section. If your tableView has more than one section either you should know your section index or go for below methods.

备选:1

UIView *contentView = (UIView *)[sender superview];
UITableViewCell *cell = (UITableViewCell *)[contentView superview];
NSIndexPath *indexPath = [tblView indexPathForCell:cell];

替代方案:2

CGPoint touchPoint = [sender convertPoint:CGPointZero toView:tblView];
NSIndexPath *indexPath = [tblView indexPathForRowAtPoint:touchPoint];
UITableViewCell *cell = [tblView cellForRowAtIndexPath:indexPath];

这篇关于如何通过iOS中的参数将UITableView IndexPath传递给UIButton @selector?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆