2

我有一個自定義的UITableview,它被劃分爲幾個部分,我已經實現了UISwipeGestureRecognizer。當我滑動我的表格視圖單元格時,出現UIButton。我現在面臨的問題是當我滑動第一個表格視圖單元格時,連續部分中的另一個單元格也會識別滑動手勢。我無法找到如何在沒有其他部分的其他單元格被滑動的情況下輕掃特定部分的單元格。如何在一個部分的特定行中滑動tableviewcell

我只想刷卡,我不想刪除/插入或添加複選標記。

UPDATE ....這是我customCell.m 泰伯維= customTableView

 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
     { 
      self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
      if (self) 
      { 
       self.backgroundColor = [UIColor clearColor]; 
       self.contentView.backgroundColor = [UIColor clearColor]; 

       self.symptomCellImageView.contentMode=UIViewContentModeScaleToFill; 


       swipeButton = [[UIButton alloc]init]; 
       swipeButton .frame = CGRectMake(220.0, 8.0, 30.0, 30.0); 
       swipeButton .hidden = NO; 

} 

在我MainViewController.m

-(void)viewDidLoad 
    {   

     symptomSwipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandleLeft)]; 
     symptomSwipeLeft .numberOfTouchesRequired = 1; 
     symptomSwipeLeft .direction = UISwipeGestureRecognizerDirectionLeft; 
     [customTableView addGestureRecognizer:symptomSwipeLeft]; 
     [self addGestureRecognizer:symptomSwipeRight]; 

      symptomSwipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandleRight)]; 
      symptomSwipeRight.numberOfTouchesRequired = 1; 
      symptomSwipeRight.direction = UISwipeGestureRecognizerDirectionRight; 
      [customTableView addGestureRecognizer:symptomSwipeRight]; 
      [self addGestureRecognizer:symptomSwipeRight]; 

    } 


- (void)swipeHandleLeft:(UISwipeGestureRecognizer *)aSwipeGestureRecognizer 
{ 
    CGPoint location = [aSwipeGestureRecognizer locationInView:customTableView ]; 
    NSIndexPath * indexPath = [customTableView indexPathForRowAtPoint:location]; 

    if(indexPath) 
{ 
     UITableViewCell * cell = (UITableViewCell *)[customTableView cellForRowAtIndexPath:indexPath]; 
     [cell.swipeButton addTarget:self action:@selector(secondPageButton:) forControlEvents:UIControlEventTouchUpInside]; 

       } 
} 



- (void)swipeHandleRight:(UISwipeGestureRecognizer *)aSwipeGestureRecognizer 
{ 
    CGPoint location = [aSwipeGestureRecognizer locationInView:customTableView ]; 
    NSIndexPath * indexPath = [customTableViewindexPathForRowAtPoint:location]; 

    if(indexPath) 
{ 
     UITableViewCell * cell = (UITableViewCell *)[customTableView cellForRowAtIndexPath:indexPath]; 
     [cell.swipeButton addTarget:self action:@selector(secondPageButton:) forControlEvents:UIControlEventTouchUpInside]; 
       } 
} 

回答

3

嘗試:

- (void)handleSwipe:(UISwipeGestureRecognizer *)aSwipeGestureRecognizer; { 
    CGPoint location = [aSwipeGestureRecognizer locationInView:_tableView]; 
    NSIndexPath * indexPath = [_tableView indexPathForRowAtPoint:location]; 

    if(indexPath){ 
    UITableViewCell * cell = (UITableViewCell *)[_tableView cellForRowAtIndexPath:indexPath]; 
    [cell whateverMethodYouWant]; 
    } 
} 

作爲一個側面說明,您正在調用多個單元的原因是因爲該行不夠獨特。在你們所有的部分都有row = 0。因此,如果你想每個單元都有一個唯一的號碼連接到其.tag財產,你需要知道的任何部分的最大行數(稱之爲largestNumberOfRows),然後計算:

cell.swipeButton.tag = (indexPath.section * largestNumberOfRows) + indexPath.row;

希望有所幫助!

編輯:

使用上述方法,在你viewDidLoad;方法,添加以下代碼:

UISwipeGestureRecognizer * recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; 
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft)]; 
[_tableView addGestureRecognizer:recognizer]; 

EDIT 2

看着你代碼,你已經把你的手勢識別器放在了錯誤的文件。下面是設置:

在你MainViewController.m文件:

- (void)viewDidLoad; { 
    [super viewDidLoad]; 
    UISwipeGestureRecognizer * recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; 
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft)]; 
    [_tableView addGestureRecognizer:recognizer]; 
} 

- (void)handleSwipe:(UISwipeGestureRecognizer *)aSwipeGestureRecognizer; { 
    CGPoint location = [aSwipeGestureRecognizer locationInView:_tableView]; 
    NSIndexPath * indexPath = [_tableView indexPathForRowAtPoint:location]; 

    if(indexPath){ 
    UITableViewCell * cell = (UITableViewCell *)[_tableView cellForRowAtIndexPath:indexPath]; 

    if(aSwipeGestureRecognizer.direction == UISwipeGestureRecognizerDirectionRight){ 
     [cell symptomCellSwipeRight]; 
    } 
    else if(aSwipeGestureRecognizer.direction == UISwipeGestureRecognizerDirectionLeft){ 
     [cell symptomCellSwipeLeft]; 
    } 
    } 
} 

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath; { 
    // Initial your cell. Then add: 
    [cell.swipeButton addTarget:self action:@selector(secondPageButton:) forControlEvents:UIControlEventTouchUpInside]; 
} 

在你PPsymptomTableCell.m文件:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier; { 
    if((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])){ 
    self.backgroundColor = [UIColor clearColor]; 
    self.contentView.backgroundColor = [UIColor clearColor]; 

    self.symptomCellImageView.contentMode=UIViewContentModeScaleToFill; 

    // SHARE ON FACEBOOK BUTTON // 

    shareFacebookButton = [[UIButton alloc]init]; 
    shareFacebookButton.frame = CGRectMake(220.0, 8.0, 30.0, 30.0); 
    shareFacebookButton.hidden = NO; 

    // DISPLAY NOTIFICATION IMAGE // 

    selectedCellImageDisplay = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"selectedSymptomImage.png"]]; 
    selectedCellImageDisplay.frame = CGRectMake(240.0, 8.0, 30.0, 30.0); 

    // SYMPTOM NAME // 

    symptomCellLabel=[[UILabel alloc]initWithFrame:CGRectMake(15.0,0.0 ,280.0,40.0)]; 
    symptomCellLabel.font=[UIFont fontWithName:@"Rockwell" size:17]; 
    symptomCellLabel.textColor=[UIColor blackColor]; 
    symptomCellLabel.backgroundColor=[UIColor clearColor]; 

    [self.contentView addSubview:symptomCellLabel]; 
    [self.contentView addSubview:selectedCellImageDisplay]; 
    [self.contentView addSubview:shareFacebookButton]; 
    } 
    return self; 
} 

- (void)symptomCellSwipeLeft; { 
    [UIView beginAnimations:@"HideView" context:nil]; 
    symptomCellLabel.frame = CGRectMake(-183.0, 0.0, 280.0, 40.0); 
    selectedCellImageDisplay.frame = CGRectMake(-340.0, 8.0, 30.0, 30.0); 
    shareFacebookButton.frame = CGRectMake(120.0, 8.0, 80.0, 30.0); 
    shareFacebookButton.hidden = NO; 
    shareFacebookButton.backgroundColor = [UIColor redColor]; 

    [UIView setAnimationDuration:0.3f]; 
    [UIView commitAnimations]; 
} 


- (void)symptomCellSwipeRight; { 
    [UIView beginAnimations:@"HideView" context:nil]; 
    symptomCellLabel.frame = CGRectMake(15.0,0.0 ,280.0,40.0); 
    selectedCellImageDisplay.frame = CGRectMake(240.0, 8.0, 30.0, 30.0); 
    shareFacebookButton.frame = CGRectMake(220.0, 8.0, 30.0, 30.0); 
    shareFacebookButton.hidden = YES; 

    [UIView setAnimationDuration:0.3f]; 
    [UIView commitAnimations]; 
} 

這應該得到的一切工作很好。

+0

..不應該叫[單元格。swipeButton addTarget:自我行動:@選擇(intoView :) forControlEvents:UIControlEventTouchUpInside]中的cellForRowAtIndexPath 和標籤應的cellForRowAtIndexPath做正確的 – raptor 2013-04-22 17:15:11

+0

我的代碼將你的'UISwipeGestureRecognizer',給你的用戶刷卡確切的細胞。從那裏,你可以調用你喜歡的任何方法。因此,如果你想要出現滑動按鈕,只需調用'cell.swipeButton.hidden =!cell.swipeButton.hidden'(這將使按鈕隱藏,如果它隱藏或反之亦然)。你甚至可以調用一個自定義的單元格方法來使按鈕變成動畫!根本不需要'.tag'屬性! – msgambel 2013-04-22 17:21:57

+0

謝謝:)...我會研究它..只是再一件事情,如果我在第一節中刷單元格,然後單元格在其他部分不會被刷卡正確 – raptor 2013-04-22 17:23:20

0

您正在使用sections執行您的表格,然後將您的標記值組合爲indexPath.sectionsindexPath.row

但請確保您實現的tagValue不重疊。

相關問題