Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button inside a custom uitableviewcell not working

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

like image 598
Vishnu Avatar asked Jul 22 '13 10:07

Vishnu


1 Answers

[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.

like image 141
Divyam shukla Avatar answered Sep 28 '22 06:09

Divyam shukla