How to pass UITableView IndexPath to UIButton @selector by parameters in iOS?

前端 未结 5 1519
无人共我
无人共我 2020-12-06 02:34

I have added UIButton in UITableViewCells. I have when the user clicks the button we have get the indexpath to use the values from NSMutableA

相关标签:
5条回答
  • 2020-12-06 03:14

    Add Your UIButton Like this

    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];
    }
    

    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.

    Alternative:1

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

    Alternative:2

    CGPoint touchPoint = [sender convertPoint:CGPointZero toView:tblView];
    NSIndexPath *indexPath = [tblView indexPathForRowAtPoint:touchPoint];
    UITableViewCell *cell = [tblView cellForRowAtIndexPath:indexPath];
    
    0 讨论(0)
  • 2020-12-06 03:14

    Given that the button is a subview of the cell view, and that the table view knows the index path of the cell view, something like this would work:

    - (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);
        }
    }
    
    0 讨论(0)
  • 2020-12-06 03:21

    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.

    0 讨论(0)
  • for me works if add tag to button in the cellForRowAtIndexPath...

    cell.button.tag = indexPath.row;
    

    and in the cell class get the tag... For example:

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

    whit that you can know the cell.row. well this works for me, Sorry for my english.

    0 讨论(0)
  • 2020-12-06 03:35

    If you only need the table row, then you can set the button's tag property to the row number. If you need the whole path (with section number) then you'll have to subclass UIButton and make a new property ("indexPath" would be a good name) that you would set when you add the button to the table row.

    0 讨论(0)
提交回复
热议问题