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

    xiaoxiao2021-03-25  44

    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);

    //}

    转载请注明原文地址: https://ju.6miu.com/read-50348.html

    最新回复(0)