2013-10-02 2 views
0

내 UITableViewCell에있는 버튼에 문제가 있습니다. 스토리 보드를 사용하고 IB를 통해 내 버튼을 연결했습니다. 내 cellforRowatIndexPath 내에서 내 버튼에 동작을 추가 :테이블보기 내의 UIButton 셀 문제

cell.likeBtnPressed.tag = indexPath.row; 
[cell.likeBtnPressed addTarget:self action:@selector(userDidTapOnLikeButton:photo:) forControlEvents:UIControlEventTouchUpInside]; 

당신은 버튼을 누를 때 호출되는 것을 볼 수 아래 :

:

-(void)userDidTapOnLikeButton:(UIButton *)button photo:(PFObject *)photo{ 
    //Disable the button so users cannot send duplicat requests 
    [button setEnabled:NO]; 
    [button setTintColor:[UIColor redColor]]; 


    //Set the new state of the button 
    BOOL liked = !button.selected; 
    [button setEnabled:liked]; 

    //Get the current number of likes the post have 
    NSString *originalButtonTitle = button.titleLabel.text; 
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; 
    [numberFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]]; 

    //Update the like count in the ECDCache 
    NSNumber *likeCount = [numberFormatter numberFromString:button.titleLabel.text]; 
    if (liked) { 
     likeCount = [NSNumber numberWithInt:[likeCount intValue] + 1]; 
     [[ECDCache sharedCache] incrementLikerCountForPhoto:photo]; 
    }else{ 
     if ([likeCount intValue] > 0) { 
      likeCount = [NSNumber numberWithInt:[likeCount intValue] - 1]; 
     } 

     [[ECDCache sharedCache] decrementLikerCountForPhoto:photo]; 
    } 

    //Add the current user as a liker of the photo in ECDCache 
    [[ECDCache sharedCache] setPhotoIsLikedByCurrentUser:photo liked:liked]; 

    //Update the button label 
    [button setTitle:[numberFormatter stringFromNumber:likeCount] forState:UIControlStateNormal]; 

    //Call the appropriate static method to handle creating/deleting the right object 
    if (liked) { 
     [ECDUtility likePhotoInBackground:photo block:^(BOOL succeeded, NSError *error) { 
      [button setEnabled:YES]; 
      [button setTitleEdgeInsets:UIEdgeInsetsMake(-1.0f, 0.0f, 0.0f, 0.0f)]; 
      [[button titleLabel] setShadowOffset:CGSizeMake(0.0f, -1.0f)]; 

      if (!succeeded) { 
       // Revert the button title (the number) if the call fails 
       [button setTitle:originalButtonTitle forState:UIControlStateNormal]; 
      } 
     }]; 
    } 
} 

I 버튼을 누르면 지금까지 내가 이것을받을 때

-[UITouchesEvent objectId]: unrecognized selector sent to instance 0x15ee5de0 

난 당신이 더 t으로 대상을 추가 할 수 없습니다 내가 잘못

답변

1

문제는 당신의 방법을 받고 두 번째 매개 변수는 PFObject하지만 UIEvent되지 않는 것입니다.

  • @selector(a) :

    는 선택기의 세 가지 유형이 addTarget:action:forControlEvents:로 보낼 수 있습니다이 방법은 매개 변수를 사용하지 않습니다.

  • @selector(a:) :이 메서드에는 컨트롤 이벤트를받은 UIControl 인 하나의 매개 변수가 있습니다.
  • @selector(a:b:) :이 메서드에는 컨트롤 이벤트를받은 UIControl과 트리거 한 UIEvent의 두 매개 변수가 있습니다.

당신은 단지 버튼을 얻을 찾고 있기 때문에,이 같은 서명이 있어야합니다

-(void)userDidTapOnLikeButton:(UIButton *)button 
{ 
    PFObject *photo = [self someLogicToGetThePhotoFromTheButton:button]; 
    ... 
+0

좋아 I입니다 이것을 시도 할 것이다. 내 userDidtap .. 메서드 내에서 단추 인덱스 경로를 검색 할 수있는 방법이 있나요? –

+0

'button'을 가져 와서'superview' (그리고 아마도 superview의 수퍼 뷰)를 보면서'UITableViewCell'을 얻습니다. 그렇다면 단지 [- UITableView indexPathForCell :]'이 필요합니다. –

+0

이론적으로,'- [UITableView indexPathForRowAtPoint :]'와 함께'@selector (a : b :)'옵션에서'UIEvent' 매개 변수를 사용하여 인덱스 경로를 얻을 수 있습니다. '[tableView indexPathForRowAtPoint : [event.allTouches.anyObject locationInView : tableView]]' –

0

무엇을했는지 모르겠어요 UIButton 선택기에서 하나의 매개 변수를 사용합니다. 선택기가 호출 될 때 sender 매개 변수만을 수신합니다.이 경우 UIButton 객체입니다. 따라서 다음을 사용하는 것이 좋습니다.

[cell.likeBtnPressed addTarget:self action:@selector(userDidTapOnLikeButton:photo:) forControlEvents:UIControlEventTouchUpInside]; 

-(void)userDidTapOnLikeButton:(id)sender{ 
    //Your code 
} 

그런 다음 셀 태그를 사용하여 사진을 검색하십시오.

행운을 빕니다)

+0

기술적으로, 당신은 두 개의 매개 변수를 가질 수 있지만, 두 번째 매개 변수는'UIEvent' –

관련 문제