6

I have a custom TableCell and a UiButton is associated with this via IB. But the button action method always return the button index as zero

Below is my code

//Inside CellForRow
[cell.M_CtrlBtnChat addTarget: self
                       action: @selector(buttonPressed:withEvent:)
             forControlEvents: UIControlEventTouchUpInside];


- (void) buttonPressed: (id) sender withEvent: (UIEvent *) event
{

    UITouch * touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView: self.tableView];
    NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint: location];
    NSLog(@"%d", indexPath.row);
}

Here index path always return zero. Any idea . Please help me

Vishnu
  • 208
  • 5
  • 19
  • What is `self`? Your table view cell? Does it have a `tableView` property? What happens when you `NSLog(@"%@", self.tableView)`? – Cyrille Jul 22 '13 at 11:02
  • Remember, you can associate an indexPath directly to a table view cell by using associated objects. – Cyrille Jul 22 '13 at 11:03
  • @Vishnu,your code is correct.Can you post your 'cellForIndexPath' method – IKKA Jul 22 '13 at 12:57

4 Answers4

5
[cell.M_CtrlBtnChat addTarget: self
                       action: @selector(buttonPressed:withEvent:)
             forControlEvents: UIControlEventTouchUpInside];
 cell.M_CtrlBtnChat.tag = indexPath.row;


- (void) buttonPressed: (id) sender withEvent: (UIEvent *) event
{

    UIButton *btn = (UIButton *)sender;
    NSLog(@"btn.tag %d",btn.tag);
    UITouch * touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView: self.tableView];
    NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint: location];
    NSLog(@"%d", indexPath.row);
}

try this it may help you.

Divyam shukla
  • 2,038
  • 14
  • 18
  • @Divz can u please give me reson for both NSLog(@"btn.tag %d",btn.tag); and NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint: location];...? both are not same.. value comming? – Nitin Gohel Jul 22 '13 at 11:13
  • @NitinGohel when you are getting location point of touch area it always returns the same value because it returns the value from the superview not from the tableview and the superview is UITableViewCell – Divyam shukla Jul 22 '13 at 11:18
  • Good answer, though I have to do some modification to make it work – AkshayT Sep 25 '13 at 07:45
2

I had the same problem and tried EVERYTHING. The thing that finally did it was implementing the method

(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

Just return whatever the height is of your custom cell (look in the NIB file)

Jeff Grimes
  • 2,300
  • 1
  • 23
  • 23
  • Thank you very much mate ! Your answer will lead me to fix my bug and saved many hours to go to waste ! – Emin Turk Jul 04 '19 at 17:50
1
- (void) buttonPressed: (id) sender withEvent: (UIEvent *) event
{
     CGPoint touchPoint = [sender convertPoint:CGPointZero toView:self.mMessagesTable];
     NSIndexPath *indexPath = [self.mMessagesTable indexPathForRowAtPoint:touchPoint];
     NSLog(@"%d", indexPath.row);
}

code for button programmatically

UIButton *mNewMsgDwn = [UIButton buttonWithType:UIButtonTypeCustom];
[mNewMsgDwn setImage:[UIImage imageNamed:@"new_message.png"] forState:UIControlStateNormal];
[mNewMsgDwn addTarget:self action:@selector(newMsg) forControlEvents:UIControlEventTouchUpInside];
mNewMsgDwn.frame = CGRectMake(179, 357, 137, 27);
[self.view addSubview:mNewMsgDwn];
iPhone 7
  • 1,731
  • 1
  • 27
  • 63
0

try it out using this bellow code..

- (void) buttonPressed: (id) sender withEvent: (UIEvent *) event
{
    UIButton *button = (UIButton *)sender;
    UITableViewCell *cell = (UITableViewCell *)button.superview;
    UITableView *tableView = (UITableView *)cell.superview;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    NSLog(@"%d", indexPath.row);
}

OR

UIButton *button = (UIButton *)sender;
CGRect buttonFrame = [button convertRect:button.bounds toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonFrame.origin];

OR Whole Example with add Custom Button in cell programatically

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
         .......
    cell.textLabel.text = [NSString stringWithFormat:@"Cell #%i", indexPath.row + 1];

    //Create the button and add it to the cell
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self
               action:@selector(customActionPressed:)
     forControlEvents:UIControlEventTouchDown];
    [button setTitle:@"Custom Action" forState:UIControlStateNormal];
    button.frame = CGRectMake(150.0f, 5.0f, 150.0f, 30.0f);
    [cell addSubview:button];

    return cell;
}

and get index path with bellow code...

//Get the superview from this button which will be our cell
UITableViewCell *tblCell = (UITableViewCell*)[sender superview];

//From the cell get its index path.
NSIndexPath *indexPathE = [myTableView indexPathForCell:tblCell];
NSLog(@"%d", indexPathE.row);
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • oh now i see your question and for that i see my another link option for ur this requirement http://stackoverflow.com/questions/2863940/how-can-i-keep-track-of-the-index-path-of-a-button-in-a-table-view-cell see this different answer.. – Paras Joshi Jul 22 '13 at 11:56
  • if you create whole 3 files for that UITableViewCell like .h,.m and xib then in .m class if you set value using custom method then it is worked for me with different tag... i created that code for client requirement if you use this flow then just tell me i will post answer.. – Paras Joshi Jul 22 '13 at 11:58