Как я могу получить indexPath
от editActionsForRowAtIndexPath:
и использовать переменные в отдельном способе (addSpeciesToFavoriteForCell
)?Получить indexPath в editActionsForRowAtIndexPath: и использовать его в другом месте?
// Swipe to left for favorite
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *favAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault
title:@"Favorite"
handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[self tableView:tableView addSpeciesToFavoriteForCell:cell];
self.tableView.editing = NO;
}];
favAction.backgroundColor = [UIColor greenColor];//color of favorite
return @[favAction];
}
- (void)tableView:(UITableView *)tableView addSpeciesToFavoriteForCell:(UITableViewCell *)cell {
UILabel *comNameLabel = (UILabel *)[cell viewWithTag:100];
NSFetchRequest *findExisting = [[NSFetchRequest alloc] init];
[findExisting setEntity:
[NSEntityDescription entityForName:@"Favorite" inManagedObjectContext:self.managedOjbectContext]];
[findExisting setPredicate:[NSPredicate predicateWithFormat:@"name == %@",comNameLabel.text]];
NSError *error;
NSArray *matchedRecords = [self.managedOjbectContext executeFetchRequest:findExisting error:&error];
if ([matchedRecords count]!=0) return;
Favorite *favEntity = [NSEntityDescription insertNewObjectForEntityForName:@"Favorite" inManagedObjectContext:self.managedOjbectContext];
favEntity.type = [NSNumber numberWithInt:0];
favEntity.name = comNameLabel.text;
if (![self.managedOjbectContext save:&error]) {
//NSLog(@"Error: %@", error);
abort();
}
}
Почему бы просто не пропустить указательный путь к методу, а не к ячейке? Параметр 'tableview' не нужен, так как это свойство в этом классе. – vadian
я делаю это vadian.thank. –