Objective c 如何在iOS中通过参数将UITableView IndexPath传递给UIButton@selector?

Objective c 如何在iOS中通过参数将UITableView IndexPath传递给UIButton@selector?,objective-c,ios,uitableview,parameters,uibutton,Objective C,Ios,Uitableview,Parameters,Uibutton,我在UITableViewCells中添加了UIButton。当用户单击按钮时,我们得到了使用NSMutableArray中的值的indexpath。我使用了下面的方法来获取当前的IndexPath [getMeButton addTarget:self action:@selector(resendTheErrorMessage:) forControlEvents:UIControlEventTouchUpInside]; -(void) resendTheErrorMessage:(id

我在
UITableViewCells
中添加了
UIButton
。当用户单击按钮时,我们得到了使用
NSMutableArray
中的值的indexpath。我使用了下面的方法来获取当前的
IndexPath

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

-(void) resendTheErrorMessage:(id)sender 
{
   NSLog(@"SEnder: %@", sender);
   //NSLog(@"Index Path : %@", indexpath);
}
有人能帮我传递当前的indexpath
UIButton的
@选择器吗。提前谢谢

编辑:

这是我从
NSLog()



如果只需要表格行,则可以将按钮的标记属性设置为行号。如果您需要整个路径(带有节号),那么您必须对UIButton进行子类化,并创建一个新属性(“indexPath”将是一个很好的名称),当您将按钮添加到表行时可以设置该属性。

像这样添加您的
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];
然后得到它的标签号-

-(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只有一个部分时,上述解决方案将起作用。如果tableView有多个分区,您应该知道分区索引或使用以下方法。

备选方案: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];

假设按钮是单元视图的子视图,并且表视图知道单元视图的索引路径,那么类似这样的操作将起作用:

- (UITableViewCell *)containingCellForView:(UIView *)view
{
    if (!view.superview)
        return nil;

    if ([view.superview isKindOfClass:[UITableViewCell class]]) 
        return (UITableViewCell *)view.superview;

    return [self containingCellForView:view.superview];
}

- (IBAction)buttonClicked:(id)sender
{
    UITableViewCell *containingCell = [self containingCellForView:sender];
    if (containingCell) {
        NSIndexPath *indexPath = [self.tableView indexPathForCell:containingCell];
        NSLog(@"Section: %i Row: %i", indexPath.section, indexPath.row);
    }
}

如果在cellForRowAtIndexPath中向按钮添加标记,则对我有效

cell.button.tag = indexPath.row;
在cell类中获取标记。。。例如:

 NSLog(@"tag number is = %d",[sender tag]);
这样你就可以知道cell.row了。
这对我很有用,对不起我的英语

对于具有多个部分的tableview,更简单的解决方案是分配标记:

cell.button.tag = indexPath.section * 1000 + indexPath.row
在事件处理程序中,请使用:

let section = sender.tag / 1000
let row = sender.tag % 1000

请注意,如果任何部分可以包含1000行或更多行,则需要乘以大于1000的数字。

您可以根据单元格的行数为按钮设置标记,然后轻松获取单元格索引。显示在单元格上添加按钮的代码(CellForRowatineXpath:)方法?@VakulSaini谢谢。我没有从(id)发件人处获取cellIndex。我已编辑我的问题,请查看此。谢谢。您之所以收到此消息,是因为您正在打印
ui按钮的id
。请参阅我的帖子以获取cellIndexpossible-replicate-of-tags在我看来是一个糟糕的解决方案。@jrturton-不错的解决方案。。。。我投票支持你。伟大的这将起作用,但您必须确保该按钮是表格单元格的直接子视图(通常是这样)。并且将子视图添加到直接单元格。视图不好。。。!我们将其添加到cell.contentView。。。。好主意是好的:-)如果您将其设置为单元格的附属视图,这是好主意-原始问题没有指定添加位置。很明显,如果视图层次结构中有n个级别,那么您必须返回n个级别才能进入单元视图。这正是我多个部分所需要的!帮我省去了很多挫折谢谢!很好的解决方案,可以处理普通或分组的表视图。我认为这不起作用。好吧,你不能在一个动作上传递你自己的参数:@selector?哪个部分不起作用?不需要额外的参数;其思想是在按钮对象添加到表中时,通过标签属性或子类的自定义属性对其本身的索引信息进行编码。tag方法是这个问题的公认答案中使用的方法。第一种方法不起作用,因为在这个方法中,您必须知道节索引。。。。但另一种解决办法会奏效。。。。。。
let section = sender.tag / 1000
let row = sender.tag % 1000