iOS tatableViewCell添加button并获取点击事件

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];        

btn.frame = CGRectMake(cell.frame.size.width-70,20, 50.0f, cell.frame.size.height-60);    

[btn setTitle:@"预约" forState:UIControlStateNormal];

btn.backgroundColor =[UIColor redColor];

[btn addTarget:self action:@selector(cellBtnClicked:event:) forControlEvents:UIControlEventTouchUpInside];    

//[btn3 addTarget:self action:@selector(onClick3:) forControlEvents:UIControlEventTouchUpInside];

//btn3.tag=indexPath.row;

[cell.contentView addSubview:btn];


- (void)cellBtnClicked:(id)sender event:(id)event {

    NSSet *touches =[event allTouches];

    UITouch *touch =[touches anyObject];

    CGPoint currentTouchPosition = [touch locationInView:_tableView];

    NSIndexPath *indexPath= [_tableView indexPathForRowAtPoint:currentTouchPosition];

    if (indexPath!= nil) {

        // do something

        NSLog(@"%zd",indexPath.row);

    }

}

//-(void)onClick3:(UIButton *) sender{

//    NSLog(@"%ld",sender.tag);

//}


你可能感兴趣的:(UITableVIew,表示图)