Saad Chaudhry
Saad Chaudhry

Reputation: 1412

how to Delete data from table view row and core data entity as well?

I have a table view which is displaying the list of records from the entity.

If i delete any record it should be deleted from both tableview and entity

This is mu IMSCategoryViewController.h file

#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

While the implementation file is

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

The Code is working pretty well. It is displaying the records from the entity named as Category.

Now what should be done here where i want to delete records according to above code...

- (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
    }   
}

if i try this code

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

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

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

it gives me an exception

-[NSFetchRequest delete:]: unrecognized selector sent to instance 0x74474a0

and an error on build No visible @interface for 'NSArray' declares the selector 'removeObjectAtIndex:' on this line of code.

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

your solutions will be much appreciated.

Bundle of thanks in advance.

Upvotes: 0

Views: 1992

Answers (2)

Vishal Singh
Vishal Singh

Reputation: 4480

@property(nonatomic,retain)NSArray *arr;

arr is immutable array, You can not add or remove objects from it.

Upvotes: 1

RFG
RFG

Reputation: 2902

I think that, in order to delete an object from an array, your arr must de an NSMutableArray instead of an NSArray.

Upvotes: 0

Related Questions