IOS-CoreData
#import <UIKit/UIKit.h> #import "RQHAppDelegate.h" @interface MainTableViewController : UITableViewController { NSMutableArray *_peoples; RQHAppDelegate *_appDelegate; } @end
#import "MainTableViewController.h" #import "People.h" #import "AppendViewController.h" @implementation MainTableViewController #pragma mark - View lifecycle //查 - (void)selectAllPeople { NSFetchRequest *request = [[NSFetchRequest alloc]init]; NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"People" inManagedObjectContext:_appDelegate.managedObjectContext]; [request setEntity:entityDescription]; NSArray *arr = [_appDelegate.managedObjectContext executeFetchRequest:request error:nil]; _peoples = [NSMutableArray arrayWithArray:arr]; [self.tableView reloadData]; } - (void)viewDidLoad { [super viewDidLoad]; _peoples = [NSMutableArray arrayWithCapacity:0]; _appDelegate = [UIApplication sharedApplication].delegate; [self selectAllPeople]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _peoples.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } People *people = [_peoples objectAtIndex:indexPath.row]; cell.textLabel.text = people.name; // Configure the cell... return cell; } - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { AppendViewController *append = segue.destinationViewController; //增 if ([segue.identifier isEqualToString:@"add"]) { append.block = ^(NSString *text){ People *people = [NSEntityDescription insertNewObjectForEntityForName:@"People" inManagedObjectContext:_appDelegate.managedObjectContext]; people.name = text; [_appDelegate saveContext]; [_peoples addObject:people]; [self.tableView reloadData]; }; } else//改 { UITableViewCell *cell = sender; NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; People *people = [_peoples objectAtIndex:indexPath.row]; append.block = ^(NSString *text){ people.name = text; [_appDelegate saveContext]; NSArray *arr = [NSArray arrayWithObject:indexPath]; [self.tableView reloadRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationAutomatic]; }; append.textViewText = people.name; } } //删 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { [_appDelegate.managedObjectContext deleteObject:[_peoples objectAtIndex:indexPath.row]]; [_appDelegate saveContext]; [_peoples removeObjectAtIndex:indexPath.row]; NSArray *arr = [NSArray arrayWithObject:indexPath]; [self.tableView deleteRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationAutomatic]; } @end
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。