首页 > 代码库 > IOS-CoreData
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
IOS-CoreData
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。