0

私のiPhone UITableビューアプリケーションで

ユーザーがテーブル ビューのセル/行をスワイプするときに 2 つのボタンを追加する必要があります。

連絡先に[SMS] ボタンを送信するための[メール作成] ボタンの 1つ。

今のところ、Send Mail (Blue Cloud Button )を追加するために次のコードを実装しました

//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以下のように繰り返される

他のセルでスワイプをテーピングしている間、他のセルのボタンを表示(削除)したくない一度に1つのクラウドボタンのみ..

4

2 に答える 2

1

ボタンをXIbに保持して、それぞれのセルに追加できないのはなぜですか? クラス全体で 1 つのオブジェクトであるため、前のセルのボタンと重複することはありません。してみてください。

于 2012-11-15T12:24:24.273 に答える
0

必要なのは、クラウド ボタンを追加した lastIndexPath を格納するための 1 つの ivar です。また、新しいボタンを追加するたびに、以前に追加した 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 に答える