0

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

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

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

我现在实现了以下代码来添加发送邮件(蓝色云按钮

//Add a left swipe gesture recognizer in view Did load
      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];
       UITableViewCell *cell = [contactsTable cellForRowAtIndexPath:indexPath];


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


      //Add mail button if index path is valid
      if(indexPath)
      {

            [cell addSubview:MailButton];

      }

}

当我们在单元格上滑动时,它执行良好并将按钮添加到选定的单元格/行。

But when we swipe another cell the buttons on the previous cell are does not hide / removed重复如下

我不想在其他单元格上显示(删除)按钮,同时在另一个单元格上轻扫,一次只有一个云按钮..

4

2 回答 2

1

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

于 2012-11-15T12:24:24.273 回答
0

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

您的代码可能看起来像这样(未经测试)。

在属性的 .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
      }

}
于 2012-11-15T12:07:47.373 回答