2016-09-11 6 views
0

Как я могу получить 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(); 
    }  
} 
+0

Почему бы просто не пропустить указательный путь к методу, а не к ячейке? Параметр 'tableview' не нужен, так как это свойство в этом классе. – vadian

+0

я делаю это vadian.thank. –

ответ

0

Вы можете получить indexPath из клетки что-то вроде в addSpeciesToFavoriteForCell,

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

     // other code 
    } 
-1

Создайте переменную экземпляра (NSIndexPath) и установите его в editActionsForRowAtIndexPath.

@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]; 
}