2

사용자 정의 UITableview을 섹션으로 나누어서 UISwipeGestureRecognizer을 구현했습니다. 표보기 셀을 스 와이프하면 UIButton이 표시됩니다. 지금 직면하고있는 문제는 첫 번째 테이블보기 셀을 스 와이프 할 때 연속 섹션의 다른 셀도 스 와이프 제스처를 인식한다는 것입니다. 다른 섹션의 다른 셀을 스 와이프하지 않고 특정 섹션의 셀을 스 와이프하는 방법을 찾을 수 없습니다.섹션의 특정 행에 대해 tableviewcell을 스 와이프하는 방법

방금 ​​스 와이프하고 싶지만 삭제/삽입하거나 체크 표시를 추가하고 싶지는 않습니다.

UPDATE는 .... 이건 내 MainViewController.m에서의 TableView = 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; 

} 

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

편집 2

당신의 코드에 제스처 인식기를 잘못된 파일. 당신의 MainViewController.m 파일에서

: 당신의 PPsymptomTableCell.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]; 
} 

:

- (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을 : 자기 행동 : @selector (intoView : forControlEvents는 : UIControlEventTouchUpInside] cellForRowAtIndexPath에 와 태그가 cellForRowAtIndexPath에서 수행해야 바로 – raptor

+0

내 코드는'UISwipeGestureRecognizer'을 가지고 당신에게 사용자가 보내 주시면됩니다 정확한 셀을 줄 것이다 . 거기에서 원하는 방식으로 호출 할 수 있습니다. 따라서 스 와이프 버튼을 나타나게하려면'cell.swipeButton.hidden =! cell.swipeButton.hidden'을 호출하면됩니다 (숨겨진 버튼은 보이지 않는 경우 숨김 버튼이됩니다). 사용자 정의 셀 메서드를 호출하여 단추를 애니메이션으로 만들 수도 있습니다! '.tag' 속성은 전혀 필요 없습니다! – msgambel

+0

감사합니다 :) ... 나는 그것을 조사 할 것입니다. 첫 번째 섹션에서 셀을 스 와이프하면 다른 섹션에서 셀을 스 와이프하지 않습니다. – raptor

0

sections으로 표를 구현 한 다음 indexPath.sectionsindexPath.row의 태그 값을 조합하십시오.

하지만 tagValue를 중첩하지 않고 구현하고 있는지 확인하십시오.

관련 문제