ios - 如何从 TableView 行和核心数据实体中删除数据?

标签 ios objective-c core-data uitableview

我有一个 TableView ,它显示实体中的记录列表。

如果我删除任何记录,它应该从tableviewentity中删除

这是 mu IMSCategoryViewController.h 文件

#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>

@interface IMSCategoryViewController : UITableViewController
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@property(nonatomic,retain)NSArray *arr;
@property (readonly, strong, nonatomic) NSMutableArray *categoryArray;
@end

虽然实现文件是

IMSCategoryViewController.m

#import "IMSCategoryViewController.h"
#import "IMSAppDelegate.h"
#import "Category.h"

@interface IMSCategoryViewController ()
{
    NSManagedObjectContext *context;
}
@end

@implementation IMSCategoryViewController
@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
@synthesize categoryArray;
@synthesize arr;


- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];

    if (self) {

        // Custom initialization

    }

    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    //    [self.tableView reloadData];

    IMSAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];

    context = [appDelegate managedObjectContext];

    NSEntityDescription *category = [NSEntityDescription entityForName:@"Category" inManagedObjectContext:context];

    NSFetchRequest *request = [[NSFetchRequest alloc] init];

    [request setEntity:category];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"is_active == 1"];

    [request setPredicate:predicate];

    [request setFetchBatchSize:25];


    [request setEntity:category];

    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];

    NSArray *srotDesc = [[NSArray alloc]initWithObjects:sort, nil];

    [request setSortDescriptors:srotDesc];

    NSError *error;

    NSMutableArray *results = [[context executeFetchRequest:request error:&error] mutableCopy];

    if (results == nil) {

        //error handle here
    }

    [self setArr:results];

    NSLog(@"there is category array");

    [self.tableView reloadData];


    [self.arr count];

    }


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{


    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{


    return [self.arr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    Category *category = [self.arr objectAtIndex:indexPath.row];

    // Configure the cell...
    cell.textLabel.text = [category name];

    cell.detailTextLabel.text = [category descript];

    return cell;
}

代码运行良好。它显示来自名为“类别”的实体的记录。

现在我想根据上面的代码删除记录应该做什么...

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}

如果我尝试这段代码

    NSManagedObject *selectedObject = [self.arr objectAtIndex:[indexPath row]];
    [_managedObjectContext deleteObject:selectedObject];

    [self.arr removeObjectAtIndex:[indexPath row]];

    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];

它给了我一个异常(exception)

-[NSFetchRequest delete:]:无法识别的选择器发送到实例 0x74474a0

构建时出错 “NSArray”没有可见的 @interface 声明选择器“removeObjectAtIndex:” 在这行代码上。

[self.arr removeObjectAtIndex:[indexPath 行]];

我们将非常感谢您的解决方案。

提前致谢。

最佳答案

@property(nonatomic,retain)NSArray *arr;

arr 是不可变数组,你不能在其中添加或删除对象。

关于ios - 如何从 TableView 行和核心数据实体中删除数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19144701/

相关文章:

ios - 本地化 iPhone 应用程序中的系统按钮使用随机语言

ios - 如何在ios中按字母顺序显示搜索结果

ios - 基于对多关系中的属性检索 NSManagedObject 的最快方法

ios核心数据导入大量数据

ios - 在后台线程中恢复 NSManagedObject 关系

iphone - NSString 中的多重搜索

ios - iOS应用程序如何设置包月?

ios - UIActivityViewController 在使用 sourceView 或 barButtonItem 的 iPad 上崩溃

objective-c - 如何限制 iOS 中 NSInteger 的最大数量?

iphone - 简单 : NSArray object to Double?