2013-10-02 38 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]; 
      } 
     }]; 
    } 
} 

當過我按下按鈕,我收到這樣的:

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

我不知道我做錯了什麼

回答

1

的問題是,你的方法接受第二個參數是不是一個PFObject,但UIEvent

有三種類型的選擇,你可以發送到addTarget:action:forControlEvents:

  • @selector(a):此方法不帶參數。
  • @selector(a:):該方法有一個參數,即UIControl收到控制事件。
  • @selector(a:b:):此方法有兩個參數,UIControl收到控制事件,並UIEvent觸發它。

由於您只希望得到的按鈕,你應該有這樣的簽名:

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

好吧,我會嘗試一下。有沒有辦法讓我檢索我的userDidtap ..方法中按鈕的索引路徑? –

+0

你可以通過'button'來查看它的'superview'(也許是superview的superview)來獲取包含它的'UITableViewCell'。那麼你只需要' - [UITableView indexPathForCell:]'。 –

+0

理論上,您可以使用'@selector(a:b:)'選項中的'UIEvent'參數和' - [UITableView indexPathForRowAtPoint:]'來獲取索引路徑。 '[tableView indexPathForRowAtPoint:[event.allTouches.anyObject locationInView:tableView]]' –

0

你不能用更多的T添加目標在UIButton選擇器中有一個參數。當調用選擇器時,只接收sender參數,在這種情況下,是UIButton對象。所以我建議你使用:

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

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

然後檢索照片使用單元格的標籤,例如。

祝你好運;)

+0

從技術上講,你可以有兩個參數,但第二個參數是一個'UIEvent' –