您的位置:首页 > 移动开发 > IOS开发

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

2017-03-14 12:07 711 查看
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);

//}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: