CoreData: Deleting object

来源:互联网 发布:歌词中有狼的网络歌曲 编辑:程序博客网 时间:2024/06/03 06:21

1.Adding Edit button on the left of the NavigationBar

- (void)viewDidLoad{    [super viewDidLoad];    // Uncomment the following line to preserve selection between presentations.    // self.clearsSelectionOnViewWillAppear = NO;     // Uncomment the following line to display an Edit button in the navigation bar for this view controller.    self.navigationItem.leftBarButtonItem=self.editButtonItem;  //adding editButtonItem.    self.navigationItem.rightBarButtonItem =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(add)];}


2.  Delete record from CoreData 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{    if (editingStyle==UITableViewCellEditingStyleDelete) {        NSString *name=((People *)[self.peopleArray objectAtIndex:indexPath.row]).name;        [self.context deleteObject: [self.peopleArray objectAtIndex:indexPath.row]];        [self viewDidAppear:YES];        NSError *error;        if ([self.context save:&error]) {            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Deleted" message:[NSString stringWithFormat:@"Name:%@ has deleted",name] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];            [alert show];        }             }}


原创粉丝点击