怎么在其他方法里给UITableView cell上添加按钮?

 我想在长按cell的时候给当前长按的cell添加个修改按钮,求高手帮助!
               UILongPressGestureRecognizer *btnEdit = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];    
               btnEdit.minimumPressDuration = 1.0; //seconds  设置响应时间 
               btnEdit.delegate = self;    
               [self.tableView addGestureRecognizer:btnEdit];  //启用长按事件 
               [btnEdit release]; 

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer

{
    CGPoint point = [gestureRecognizer locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];//获取响应的长按的indexpath 
    if (indexPath == nil)
    {
        NSLog(@"没有数据"); 
    }
    else 
    {
        NSLog(@"长按的是: %d", indexPath.row); 
    //添加修改方法
    //cell.selectionStyle=UITableViewCellSelectionStyleNone;
    UIImage *buttonUpImage = [UIImage imageNamed:@"button_up.png"];
    UIImage *buttonDownImage = [UIImage imageNamed:@"button_down.png"];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0.0, 0.0, buttonUpImage.size.width,
                              buttonUpImage.size.height);
    [button setBackgroundImage:buttonDownImage
                      forState:UIControlStateNormal];
    [button setBackgroundImage:buttonUpImage
                      forState:UIControlStateHighlighted];
    [button setTitle:@"修改" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(updateWebSite:)
     forControlEvents:UIControlEventTouchUpInside];
    //cell.accessoryView = button;
    }
        
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,在CreateOutboundViewController中,你需要在点击删除按钮的时候弹出CreateOutboundDeletePopupView弹窗。可以通过在按钮的点击事件中创建弹窗视图并将其添加到当前视图上来实现: ```objc - (IBAction)deleteButtonTapped:(id)sender { CreateOutboundDeletePopupView *deletePopupView = [[CreateOutboundDeletePopupView alloc] initWithFrame:self.view.frame]; deletePopupView.delegate = self; [self.view addSubview:deletePopupView]; } ``` 接下来,你需要在CreateOutboundDeletePopupView中添加一个确定按钮,并在点击确定按钮时触发向后台请求数据的操作。可以通过在确定按钮的点击事件中调用代理方法来实现: ```objc - (IBAction)confirmButtonTapped:(id)sender { // 向后台请求数据并删除 // ... // 关闭弹窗 [self removeFromSuperview]; // 调用代理方法,通知CreateOutboundViewController删除成功 if ([self.delegate respondsToSelector:@selector(deleteSuccessful)]) { [self.delegate deleteSuccessful]; } } ``` 在CreateOutboundViewController中,需要实现代理方法deleteSuccessful,该方法将在CreateOutboundDeletePopupView中的确定按钮被点击后被调用。在该方法中,你需要更新tableview的cell,将删除后的cell的button按钮显示出来。可以通过刷新tableview来实现: ```objc - (void)deleteSuccessful { // 更新tableview [self.tableView reloadData]; } ``` 在tableview的cell中,你需要根据cell的状态来判断是否显示button按钮。可以通过在cellForRowAtIndexPath代理方法中设置cell的状态来实现: ```objc - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; // 根据cell的状态来设置button按钮是否显示 if (cell.isSelected) { cell.button.hidden = NO; cell.userInteractionEnabled = YES; } else { cell.button.hidden = YES; cell.userInteractionEnabled = NO; } return cell; } ``` 在CreateOutboundViewController中,你可以在网络请求成功后调用reloadData方法刷新tableview,并在代理方法deleteSuccessful中调用该方法: ```objc - (void)deleteSuccessful { // 向后台请求数据并删除 // ... // 更新tableview [self.tableView reloadData]; } ``` 最后,你需要在CreateOutboundDeletePopupView中添加一个代理属性,并在CreateOutboundViewController中实现该代理协议: ```objc @protocol CreateOutboundDeletePopupViewDelegate <NSObject> - (void)deleteSuccessful; @end @interface CreateOutboundDeletePopupView : UIView @property (nonatomic, weak) id<CreateOutboundDeletePopupViewDelegate> delegate; @end ``` 这样,当你在CreateOutboundDeletePopupView中的确定按钮被点击后,就可以调用代理方法通知CreateOutboundViewController删除成功,然后更新tableview的cell,将删除后的cell的button按钮显示出来。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值