0

I have implemented this button drawer and I've added some buttons to it. That said, I'm unsure how I'll go about sending messages from these buttons in the drawer to the appropriate delegate method to delete the item from my tableView.

How do I get the correct indexPath? Should I make a new NSIndex for uitableviewcells that have had their check button toggled?

Thanks

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    HHPanningTableViewCell *cell = (HHPanningTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    NSInteger directionMask = indexPath.row % 5;
    if (cell == nil) {
        cell = [[HHPanningTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];




    UIView *drawerView = [[UIView alloc] initWithFrame:cell.frame];

    drawerView.backgroundColor = [UIColor grayColor];
    cell.drawerView = drawerView;
        UIImage *checkImage = [UIImage imageNamed:@"check.png"];
        UIButton *checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [checkButton setImage:checkImage forState:UIControlStateNormal];
        cell.frame = CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height);
        checkButton.frame = CGRectMake(10, 10, checkImage.size.width, checkImage.size.height);
        [drawerView addSubview:checkButton];

        [checkButton addTarget:nil action:@selector(onCheckMarkTap:) forControlEvents:UIControlEventTouchUpInside];

    }

- (void)onCheckMarkTap {
    NSLog(@"Delete the cell");

}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        }

}
STANGMMX
  • 973
  • 7
  • 18
  • 31
  • 1
    I have answered to this similiar question: http://stackoverflow.com/questions/7504421/getting-row-of-uitableview-cell-on-button-press/7505025#7505025 – user523234 Mar 24 '13 at 02:51

3 Answers3

1

This question already has an answer. You get the coordinates of the button that was tapped in the table view's coordinate system using target/action with an event or via a gesture recognizer, then use UITableView's method -indexPathForRowAtPoint: to get the indexPath of the cell containing the button at that point.

Community
  • 1
  • 1
Andrew
  • 7,630
  • 3
  • 42
  • 51
0

Within cellForRowAtIndexPath:

checkButton.tag = indexPath.row

Also inside onCheckMarkTap, do this:

- (void)onCheckMarkTap : (id) sender
{
    if (sender.tag == 0)
    ///code
    else if (sender.tag == 1)
    ///code

}
Nirav Bhatt
  • 6,940
  • 5
  • 45
  • 89
-2

The way I handle problems like this in my own code is to subclass "UIButton", where I add in a "NSIndexPath" or "tableRow" ivar into the subclassed button. Let's name is "StangButton".

With a subclassed button, you can populate the "indexPath" ivar when you are composing the cell via "cellForRowAtIndexPath:".

Then, when the button is pressed, you'll know which button was pressed and which row it was in.

You can also check to see if the "tableView:didSelectRowAtIndexPath:" table view delegate method is called when the button is touched in the cell.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • I think I follow but, you wouldn't happen to have an example or a link to an example that follows this methodology would you? – STANGMMX Mar 24 '13 at 01:07