20

I want to detect button tap on a UITableViewCell where the parent UITableView consists of multiple sections.

I was able to do it in the case of single section, but I got into trouble determining the correct section where there were more than one.

I am trying to determine the correct section corresponding to the UITableViewCell where the user has tapped on the button.

Here is the updated code after the problem is being solved:

@property (strong, nonatomic) NSMutableArray* quantityArray;
@property (strong, nonatomic) NSMutableArray* rows; 

@synthesize quantityArray,rows;

- (void)viewDidLoad {
    [super viewDidLoad];

    quantityArray = [[NSMutableArray alloc] init];
    [self PluMinusFunction];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        //
        // Some code here.
        //         

    if (addBtnClicked || minusBtnClicked) {
        cell.lblCount.text = [[quantityArray objectAtIndex:indexPath.section-1]objectAtIndex:indexPath.row];
        addBtnClicked = NO;
        minusBtnClicked = NO;
    } else {
        cell.lblCount.text = [[quantityArray objectAtIndex:indexPath.section-1]objectAtIndex:indexPath.row];                
    }

        cell.btnMinus.tag = indexPath.row;
        cell.btnPlus.tag = indexPath.row;
        cell.lblCount.tag = indexPath.row;

        [cell.btnPlus addTarget:self action:@selector(addItem:) forControlEvents:UIControlEventTouchUpInside];

        if ([ cell.lblCount.text integerValue]!=0) {
            [cell.btnMinus addTarget:self action:@selector(deleteItem:) forControlEvents:UIControlEventTouchUpInside];
        }

        return cell;
    }
}

#pragma mark - Plus Minus Button

- (void)PluMinusFunction {
    for (int j=0; j <[itemArr count]; j++) {
        rows = [[NSMutableArray alloc] init];
        for (int i=0; i <[ [[itemArr objectAtIndex:j] objectForKey:@"itemList"]count]; i++) {
            [rows insertObject:@"0" atIndex:i];
        }
        [quantityArray insertObject:rows atIndex:j];
    }
    [self sum];
}

#pragma mark - UIButton selector

- (void)addItem:(UIButton*)button {
    CGPoint touchPoint = [button convertPoint:CGPointZero toView:self.tableView];
    NSIndexPath *clickedButtonIndexPath = [self.tableView indexPathForRowAtPoint:touchPoint];

    NSInteger row = clickedButtonIndexPath.row;
    NSInteger section = clickedButtonIndexPath.section;

    NSInteger  LabelText =[[[quantityArray objectAtIndex:section-1]objectAtIndex:row]integerValue] + 1;
    NSMutableArray *subArray = [quantityArray objectAtIndex:section-1];
    [subArray replaceObjectAtIndex:row withObject: [NSString stringWithFormat:@"%ld",(long)LabelText]];
    [quantityArray replaceObjectAtIndex:section-1 withObject: subArray];
    addBtnClicked = YES;

    NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:row inSection:section];
    NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
    [self.tableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];
}

- (void)deleteItem:(UIButton*)button {
    CGPoint touchPoint = [button convertPoint:CGPointZero toView:self.tableView];
    NSIndexPath *clickedButtonIndexPath = [self.tableView indexPathForRowAtPoint:touchPoint];

    NSInteger row = clickedButtonIndexPath.row;
    NSInteger section = clickedButtonIndexPath.section;

    NSInteger  LabelValue =[[[quantityArray objectAtIndex:section-1]objectAtIndex:row]integerValue];

    if (LabelValue >= 1) {
        NSInteger  LabelText =[[[quantityArray objectAtIndex:section-1]objectAtIndex:row]integerValue] - 1;
        NSMutableArray *subArray = [quantityArray objectAtIndex:section-1];
        [subArray replaceObjectAtIndex:row withObject: [NSString stringWithFormat:@"%ld",(long)LabelText]];

        [quantityArray replaceObjectAtIndex:section-1 withObject: subArray];
        addBtnClicked = YES;

        NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:row inSection:section];
        NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
        [self.tableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];
    }
}

I want to achieve the layout as shown below:

enter image description here

This link gave the solution to my problem too:

Nimesh Neema
  • 1,528
  • 2
  • 17
  • 44
Nischal Hada
  • 3,230
  • 3
  • 27
  • 57

4 Answers4

51

Objective-C

-(void)addItem:(UIButton*) sender
{

CGPoint touchPoint = [sender convertPoint:CGPointZero toView:mainTable]; // maintable --> replace your tableview name
NSIndexPath *clickedButtonIndexPath = [mainTable indexPathForRowAtPoint:touchPoint];

 NSLog(@"index path.section ==%ld",(long)clickedButtonIndexPath.section);

 NSLog(@"index path.row ==%ld",(long)clickedButtonIndexPath.row);


}

Swift3

 func addItem(sender: UIButton)
{
    var touchPoint = sender.convert(CGPoint.zero, to: self.maintable)
    // maintable --> replace your tableview name
    var clickedButtonIndexPath = maintable.indexPathForRow(at: touchPoint)
    NSLog("index path.section ==%ld", Int(clickedButtonIndexPath.section))
    NSLog("index path.row ==%ld", Int(clickedButtonIndexPath.row))


}

Swift2 and above

func addItem(sender: UIButton)
 {
var touchPoint: CGPoint = sender.convertPoint(CGPointZero, toView: mainTable)
    // maintable --> replace your tableview name
var clickedButtonIndexPath: NSIndexPath = mainTable(forRowAtPoint: touchPoint)
NSLog("index path.section ==%ld", Int(clickedButtonIndexPath.section))
NSLog("index path.row ==%ld", Int(clickedButtonIndexPath.row))
}
Amir_P
  • 8,322
  • 5
  • 43
  • 92
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • i Got the issue > in some case any ideas ab that bro – Nischal Hada Jul 27 '15 at 10:03
  • this is occurring in random scenario , which scenario u faced – Anbu.Karthik Jul 27 '15 at 10:06
  • can u check this question http://stackoverflow.com/questions/31692692/what-could-we-do-in-case-of-custom-cell-in-viewforheaderinsection-to-reload-part – Nischal Hada Jul 29 '15 at 06:43
  • Swift code that works is let touchPoint: CGPoint = sender.convertPoint(CGPointZero, toView: self.tableView) let buttonIndexPath: NSIndexPath = self.tableView.indexPathForRowAtPoint(touchPoint)! – richc Jun 02 '16 at 08:29
  • Swift 3.1 - var clickedButtonIndexPath = mainTable.indexPathForRow(at: touchPoint) – Wizkid Apr 15 '17 at 14:00
1
Swift 3 

let clickedButtonIndexPath = self.tableView.indexPathForRow(at: touchPoint)
        if(clickedButtonIndexPath != nil)
        {
            NSLog("index path.section ==%ld", Int(clickedButtonIndexPath!.section))
            NSLog("index path.row ==%ld", Int(clickedButtonIndexPath!.row))
           }
khushboo
  • 260
  • 2
  • 12
1

Swift 5

 let touchPoint: CGPoint = sender.convert(.zero, to: tableViews)
    let clickedButtonIndexPath = self.tableViews.indexPathForRow(at: touchPoint)
            if(clickedButtonIndexPath != nil)
            {
                NSLog("index path.section ==%ld", Int(clickedButtonIndexPath!.section))
                NSLog("index path.row ==%ld", Int(clickedButtonIndexPath!.row))
               }
Chandan Jee
  • 5,440
  • 4
  • 16
  • 24
0

A more direct way to get the index path without using CGPoint and making assumptions about UI is to look at the .superview. I'd imagine this is more reliable in practice.

- (myTableViewCell *)cellContainingView:(UIView *)view
{
    do {
        view = view.superview;
    } while (view != nil && ![view isKindOfClass:[myTableViewCell class]]);

    return (myTableViewCell *)view;
}

Then you can simply do this in the button action:

- (IBAction)myButtonPressed:(id)sender
{
    myTableViewCell *cell = [self cellContainingView:(UIView *)sender];
    NSIndexPath *path = [self.tableView indexPathForCell:cell];
}
Nestor
  • 2,753
  • 2
  • 29
  • 34