Iphone 如何在自定义滑动(而不是滑动以删除)单元格(滑动至邮件/sms)中点击一个单元格时删除在其他TableView单元格中添加的子视图

Iphone 如何在自定义滑动(而不是滑动以删除)单元格(滑动至邮件/sms)中点击一个单元格时删除在其他TableView单元格中添加的子视图,iphone,objective-c,uitableview,Iphone,Objective C,Uitableview,在我的iPhone UITable视图中应用程序 当用户在表格视图单元格/行上滑动时,我需要添加两个按钮。 一个用于撰写邮件按钮一个用于向联系人发送短信按钮 我实现了下面的代码来添加发送邮件(蓝云按钮),现在才使用 当我们在单元格上滑动时,它会执行fine并将按钮添加到所选单元格/行 但是当我们滑动另一个单元格时,上一个单元格上的按钮不会隐藏/删除如下重复 我不想在另一个单元格上显示(删除)按钮,而在另一个单元格上一次只显示一个云按钮。您需要的是一个ivar,用于存储添加云按钮的lastInde

在我的iPhone UITable视图中应用程序

当用户在表格视图单元格/行上滑动时,我需要添加两个按钮。

一个用于撰写邮件按钮一个用于向联系人发送短信按钮

我实现了下面的代码来添加发送邮件(蓝云按钮),现在才使用

当我们在单元格上滑动时,它会执行fine并将按钮添加到所选单元格/行

但是当我们滑动另一个单元格时,上一个单元格上的按钮不会隐藏/删除
如下重复


我不想在另一个单元格上显示(删除)按钮,而在另一个单元格上一次只显示一个云按钮。

您需要的是一个ivar,用于存储添加云按钮的lastIndexPath。 在每次添加新按钮时,您都必须从明显添加的indexPath中删除该按钮,并将新的indexPath分配给ivar

您的代码可能如下所示(未经测试)

属性上的in.h文件,如

@property (strong, nonatomic) NSIndexPath *lastIndexPath;
并在.m文件中合成它

现在代码将如下所示

UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self 
                                                                                       action:@selector(swipeRow:)];

      [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
      [contactsTable addGestureRecognizer:recognizer];
      [recognizer release];    





- (void)swipeRow:(UISwipeGestureRecognizer *)gestureRecognizer
{
      //Get location of the swipe
      CGPoint location = [gestureRecognizer locationInView:contactsTable];
      NSIndexPath *indexPath = [contactsTable indexPathForRowAtPoint:location];
      //New Code 
      UITableViewCell *cell = [contactsTable cellForRowAtIndexPath:indexPath];
      if(lastIndexPath != nil)
           UITableViewCell *last = [contactsTable cellForRowAtIndexPath:lastIndexPath];
      //End 
       UIButton *MailButton = [UIButton buttonWithType:UIButtonTypeCustom];
                  [MailButton setBackgroundImage:[UIImage imageNamed:@"sendData.png"]         forState:UIControlStateNormal];
                MailButton.frame=CGRectMake(150, 0, 40.0, 30.0);
         [MailButton addTarget:self action:@selector(SendMail)   forControlEvents:UIControlEventTouchUpInside];
MailButton.tag = 1111;// New code

      //Add mail button if index path is valid
      if(indexPath)
      {
            //New Code
            UIButton *mb = (UIButton *) [cell viewWithTag:1111];
            [mb removeFromSuperview];
            //End 
            [cell addSubview:MailButton];
            lastIndexPath = indexPath;// New Code
      }

}

为什么不能将按钮保留在XIb中,并将其添加到相应的单元格中?它是整个类的单个对象,因此在前面的单元格中不会有重复的按钮。请试一试

UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self 
                                                                                       action:@selector(swipeRow:)];

      [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
      [contactsTable addGestureRecognizer:recognizer];
      [recognizer release];    





- (void)swipeRow:(UISwipeGestureRecognizer *)gestureRecognizer
{
      //Get location of the swipe
      CGPoint location = [gestureRecognizer locationInView:contactsTable];
      NSIndexPath *indexPath = [contactsTable indexPathForRowAtPoint:location];
      //New Code 
      UITableViewCell *cell = [contactsTable cellForRowAtIndexPath:indexPath];
      if(lastIndexPath != nil)
           UITableViewCell *last = [contactsTable cellForRowAtIndexPath:lastIndexPath];
      //End 
       UIButton *MailButton = [UIButton buttonWithType:UIButtonTypeCustom];
                  [MailButton setBackgroundImage:[UIImage imageNamed:@"sendData.png"]         forState:UIControlStateNormal];
                MailButton.frame=CGRectMake(150, 0, 40.0, 30.0);
         [MailButton addTarget:self action:@selector(SendMail)   forControlEvents:UIControlEventTouchUpInside];
MailButton.tag = 1111;// New code

      //Add mail button if index path is valid
      if(indexPath)
      {
            //New Code
            UIButton *mb = (UIButton *) [cell viewWithTag:1111];
            [mb removeFromSuperview];
            //End 
            [cell addSubview:MailButton];
            lastIndexPath = indexPath;// New Code
      }

}