У меня проблемы с моей кнопкой, которая находится в стороне моего UITableViewCell. Я использую раскадровку и подключаю свою кнопку через IB. В моей cellforRowatIndexPath я добавил действие на мою кнопку:UIButton в Table View Cell вопрос
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
Я не уверен, что я сделал неправильно
Хорошо я попробуем это. Есть ли способ получить индексный путь для кнопки в моем методе userDidtap ..? –
Вы берете 'button' и смотрите его' superiew' (и, возможно, супервизор супервизора), чтобы получить 'UITableViewCell', который его содержит. Тогда вам просто нужно '- [UITableView indexPathForCell:]'. –
Теоретически вы можете использовать параметр 'UIEvent' из параметра' @selector (a: b:) 'вместе с' - [UITableView indexPathForRowAtPoint:] 'для получения указателя. '[tableView indexPathForRowAtPoint: [event.allTouches.anyObject locationInView: tableView]]' –