2016-09-11 5 views
0

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(); 
    }  
} 
+0

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

+0

ich mache das vadian.thank. –

Antwort

0

Sie können wie in addSpeciesToFavoriteForCellindexPath von Zelle etwas zu bekommen,

- (void)tableView:(UITableView *)tableView addSpeciesToFavoriteForCell:(UITableViewCell *)cell 
    { 
     NSIndexPath *indexPath = [tableView indexPathForCell:cell]; 

     // other code 
    } 
-1

Erstellen Sie eine Instanz-Variable (NSIndexPath) und es in editActionsForRowAtIndexPath gesetzt.

@property (strong, nonatomic) NSIndexPath *index; 


- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

index = 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]; 
} 
Verwandte Themen