Wie kann ich die indexPath
von editActionsForRowAtIndexPath:
bekommen und die Variable in einer separaten Methode (addSpeciesToFavoriteForCell
) verwenden?IndexPath in editActionsForRowAtIndexPath abrufen: und anderswo verwenden?
// 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();
}
}
Warum nicht einfach den Index Pfad zur Methode und nicht die Zelle? Und der Parameter 'tableview' wird nicht benötigt, da es sich um eine Eigenschaft in dieser Klasse handelt. – vadian
ich mache das vadian.thank. –