[英]UIButton within Table View Cell issue
我的UITableViewCell旁邊的按鈕出現問題。 我正在使用情節提要,並通過IB連接了我的按鈕。 在我的cellforRowatIndexPath中,我向按鈕添加了一個動作:
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
我不確定我做錯了什么
問題在於您的方法接收的第二個參數不是PFObject
,而是UIEvent
。
您可以將三種選擇器發送到addTarget:action:forControlEvents:
:
@selector(a)
:此方法不帶參數。 @selector(a:)
:此方法具有一個參數,即接收控件事件的UIControl
。 @selector(a:b:)
:此方法有兩個參數,一個是接收控件事件的UIControl
, UIEvent
是觸發它的UIEvent
。 由於您只想獲取按鈕,因此應該具有以下簽名:
-(void)userDidTapOnLikeButton:(UIButton *)button
{
PFObject *photo = [self someLogicToGetThePhotoFromTheButton:button];
...
您不能在UIButton選擇器中添加具有多個參數的目標。 調用選擇器時,僅接收發送者參數(在這種情況下為UIButton對象)。 因此,我建議您使用:
[cell.likeBtnPressed addTarget:self action:@selector(userDidTapOnLikeButton:photo:) forControlEvents:UIControlEventTouchUpInside];
-(void)userDidTapOnLikeButton:(id)sender{
//Your code
}
然后例如使用單元格的標簽檢索照片。
祝好運 ;)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.