在editActionsForRowAtIndexPath中获取indexPath并在其他地方使用它?

时间:2016-09-11 06:40:21

标签: ios objective-c uitableview swipe nsindexpath

如何从indexPath获取editActionsForRowAtIndexPath:并在单独的方法中使用该变量(addSpeciesToFavoriteForCell)?

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

2 个答案:

答案 0 :(得分:0)

您可以从@RequestMapping(path = "/save/{id}", method = RequestMethod.POST) public String saveRecipe(@ModelAttribute @Valid Recipe recipe, BindingResult result, @PathVariable Long id, RedirectAttributes attributes) { recipe.setUser(getLoggedUser()); if (recipe.getCategory() != null) recipe.setCategory(categoryService.findById(recipe.getCategory().getId())); recipe.setFavoriteUsers(recipeService.findById(recipe.getId()).getFavoriteUsers()); recipe.setPhoto(recipeService.findById(recipe.getId()).getPhoto()); if (result.hasErrors()) { for (ObjectError o : result.getAllErrors()) attributes.addFlashAttribute("flash",new FlashMessage(o.toString(), FlashMessage.Status.FAILURE)); attributes.addFlashAttribute("recipe", recipe); attributes.addFlashAttribute("recipeFavorite", recipe.isFavorite(getLoggedUser())); return "redirect:/edit/"+id; } recipeService.save(recipe); attributes.addFlashAttribute("flash", new FlashMessage("Recipe saved.", FlashMessage.Status.SUCCESS)); return "redirect:/"; }

等单元格中获取indexPath
addSpeciesToFavoriteForCell

答案 1 :(得分:-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];
}