自定义uitableviewcell内的按钮不起作用

Vis*_*hnu 5 iphone objective-c uitableview ipad ios

我有一个自定义的TableCell,UiButton通过IB与此相关联.但是按钮操作方法总是将按钮索引返回为零

以下是我的代码

//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);
}
Run Code Online (Sandbox Code Playgroud)

索引路径始终返回零.任何的想法 .请帮我

Div*_*kla 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);
}
Run Code Online (Sandbox Code Playgroud)

试试这可能会对你有所帮助.


iPh*_*e 7 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);
}
Run Code Online (Sandbox Code Playgroud)

以编程方式编写按钮代码

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];
Run Code Online (Sandbox Code Playgroud)