【问题标题】:How to pass UITableView IndexPath to UIButton @selector by parameters in iOS?如何在iOS中通过参数将UITableView IndexPath传递给UIButton @selector?
【发布时间】:2012-08-09 18:57:30
【问题描述】:

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

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

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

谁能帮我传递当前索引路径UIButton's@selector。提前致谢。

编辑:

这是我从NSLog()得到的输出

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

【问题讨论】:

  • 您可以根据单元格的行数为按钮设置标签,然后您可以轻松获取单元格索引。显示您在单元格 (cellForRowAtIndexPath:) 方法上添加按钮的代码?
  • @VakulSaini 谢谢。我没有从(id)发件人那里得到 cellIndex。我已经编辑了我的问题,请查看这个。谢谢。
  • 你得到这个是因为你正在打印UIButton的id。查看我的帖子获取 cellIndex
  • How to know the UITableview row number 的可能重复项 - 在我看来,标签是一个糟糕的解决方案。
  • @jrturton - 不错的解决方案....我对此投了赞成票。太好了!

标签: objective-c ios uitableview parameters uibutton


【解决方案1】:

像这样添加你的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 只有 1 个部分时,上述解决方案将起作用。如果您的 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];

【讨论】:

  • 第一个将不起作用,因为在此您必须知道部分索引....但替代解决方案将起作用......
【解决方案2】:

鉴于按钮是单元格视图的子视图,并且表格视图知道单元格视图的索引路径,这样的事情会起作用:

- (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);
    }
}

【讨论】:

  • 这会起作用,但你必须确保按钮是表格单元格的直接子视图(通常是)。
  • 并且将subView添加到直接cell.view不好......!我们将它添加到 cell.contentView .... 想法很好 :-)
  • 最好将其设置为单元格的附属视图 - 原始问题未指定添加位置。显然,如果它在视图层次结构中向下 n 级,那么您必须向上 n 级才能到达单元格视图。
  • 正是我需要的多个部分!为我节省了很多挫折,谢谢!
  • 很好的解决方案,可以处理普通或分组的表格视图。
【解决方案3】:

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

【讨论】:

  • 我认为这行不通。 AFAIK 你不能在一个动作上传递你自己的参数:@selector?
  • 哪一部分不行?不需要额外的参数;这个想法是在将按钮对象添加到表中时,通过 tag 属性或子类的自定义属性对按钮对象本身的索引信息进行编码。 tag 方法是这个问题的公认答案中使用的方法。
【解决方案4】:

对于包含多个部分的表格视图,更简单的解决方案是分配标签:

cell.button.tag = indexPath.section * 1000 + indexPath.row

在事件处理程序中使用:

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

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

【讨论】:

    【解决方案5】:

    如果将标签添加到 cellForRowAtIndexPath 中的按钮,对我来说有效...

    cell.button.tag = indexPath.row;
    

    并在单元格类中获取标签...例如:

     NSLog(@"tag number is = %d",[sender tag]);
    

    你可以知道 cell.row。 好吧,这对我有用,对不起我的英语。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-14
      • 2021-03-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多