8

我已经添加UIButtonUITableViewCells. 我有当用户单击按钮时,我们获得了 indexpath 以使用来自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>>
4

5 回答 5

30

添加您的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];
于 2012-08-13T14:23:21.090 回答
6

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

- (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);
    }
}
于 2012-08-13T14:35:19.610 回答
2

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

于 2012-08-13T14:23:02.563 回答
2

For a tableview with more than one section a simpler solution would be to assign the tag:

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

In the event handler use:

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

Note that you need to multiply with a larger number than 1000 if any of your sections can contain 1000 or more rows.

于 2016-06-10T11:10:27.197 回答
0

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

cell.button.tag = indexPath.row;

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

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

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

于 2014-07-07T22:21:20.783 回答