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

 

posted @ 2015-01-12 20:31  等待绽放的季节  阅读(112)  评论(0编辑  收藏  举报