gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:36:15 25 4
gpt4 key购买 nike

我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com