2011-04-12 6 views
0

내 앱을 다시 실행하면 테이블보기의 셀이 복제됩니다. 추가 및 삭제되는 경우 그렇지 않으면 괜찮 등이 여기에 핵심 데이터 앱 실행시 TableView 셀 복제

내 코드입니다 :

@implementation RoutineTableViewController 

@synthesize routineTableView; 
@synthesize eventsArray; 
@synthesize entered; 
@synthesize managedObjectContext; 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
    if (managedObjectContext == nil) 
    { 
     managedObjectContext = [(CurlAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 
    } 

    NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Routine" inManagedObjectContext:managedObjectContext]; 
    [request setEntity:entity]; 

    NSError *error = nil; 
    NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy]; 
    if (mutableFetchResults == nil) { 
     // Handle the error. 
    } 
    [self setEventsArray:mutableFetchResults]; 
    [mutableFetchResults release]; 
    [request release]; 

    UIBarButtonItem * addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showPrompt)]; 
    [self.navigationItem setLeftBarButtonItem:addButton]; 
    [addButton release]; 

    UIBarButtonItem *editButton = [[UIBarButtonItem alloc]initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered target:self action:@selector(toggleEdit)]; 
    self.navigationItem.rightBarButtonItem = editButton; 
    [editButton release]; 

    [super viewDidLoad]; 
} 

- (void)viewDidUnload 
{ 
    self.eventsArray = nil; 
    [super viewDidUnload]; 
} 

-(void)toggleEdit 
{ 
    [self.routineTableView setEditing: !self.routineTableView.editing animated:YES]; 

    if (self.routineTableView.editing) 
     [self.navigationItem.rightBarButtonItem setTitle:@"Done"]; 
    else 
     [self.navigationItem.rightBarButtonItem setTitle:@"Edit"]; 
} 

- (void)dealloc 
{ 
    [managedObjectContext release]; 
    [eventsArray release]; 
    [entered release]; 
    [super dealloc]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
} 

#pragma mark - 
#pragma mark Add an event 

-(void)addEvent 
{  
    Routine *routine = (Routine *)[NSEntityDescription insertNewObjectForEntityForName:@"Routine" inManagedObjectContext:managedObjectContext]; 

    routine.name=entered; 

    NSError *error = nil; 
    if (![managedObjectContext save:&error]) { 
     // Handle the error. 
    } 
    NSLog(@"%@", error); 

    /* 
    [eventsArray insertObject:routine atIndex:0]; 

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 

    [self.routineTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

    [self.routineTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES]; 
    */ 
    } 

-(void)showPrompt 
{ 
    AlertPrompt *prompt = [AlertPrompt alloc]; 
    prompt = [prompt initWithTitle:@"Add Workout Day" message:@"\n \n Please enter title for workout day" delegate:self cancelButtonTitle:@"Cancel" okButtonTitle:@"Add"]; 
    [prompt show]; 
    [prompt release]; 
} 

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex 
{ 


    if (buttonIndex != [alertView cancelButtonIndex]) 
    { 
     entered = [(AlertPrompt *)alertView enteredText]; 

     if(eventsArray && entered) 
     { 
      Routine *tempRoutine = (Routine *)[NSEntityDescription insertNewObjectForEntityForName:@"Routine" inManagedObjectContext:managedObjectContext]; 
      tempRoutine.name = entered; 
      [eventsArray addObject:tempRoutine]; 
      [routineTableView reloadData]; 
      [self addEvent]; 
      NSLog(@"tempRoutine.name is: %@",tempRoutine.name); 
     } 
    } 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [eventsArray count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    Routine *tempRoutine = (Routine *)[eventsArray objectAtIndex:indexPath.row]; 
    cell.textLabel.text = tempRoutine.name; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

    return cell; 
} 

// Override to support conditional editing of the table view. 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the specified item to be editable. 
    return YES; 
} 

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    if (editingStyle == UITableViewCellEditingStyleDelete) { 

     // Delete the managed object at the given index path. 
     NSManagedObject *eventToDelete = [eventsArray objectAtIndex:indexPath.row]; 
     [managedObjectContext deleteObject:eventToDelete]; 

     // Update the array and table view. 
     [eventsArray removeObjectAtIndex:indexPath.row]; 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES]; 

     // Commit the change. 
     NSError *error = nil; 
     if (![managedObjectContext save:&error]) { 
      // Handle the error. 
     } 
    } 

답변

0

나는 생각한다, 우리는 두 번 추가 ....

Routine *tempRoutine = (Routine *)[NSEntityDescription insertNewObjectForEntityForName:@"Routine" inManagedObjectContext:managedObjectContext]; 
      tempRoutine.name = entered; 
     ----> [eventsArray addObject:tempRoutine]; 
      [routineTableView reloadData]; 
     ----> [self addEvent]; //comment this line in alertViewDelegate 

또는 당신은 할 수 코드 변경에 따라 사용 ..... 그것은

구문 오류

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex 
{ 


    if (buttonIndex != [alertView cancelButtonIndex]) 
    { 
     entered = [(AlertPrompt *)alertView enteredText]; 

     if(eventsArray && entered) 
     { 
      [self addEvent]; 

     } 
    } 
} 


-(void)addEvent 
{  
    Routine *routine = (Routine *)[NSEntityDescription insertNewObjectForEntityForName:@"Routine" inManagedObjectContext:managedObjectContext]; 

    routine.name=entered; 

    NSError *error = nil; 
    if (![managedObjectContext save:&error]) { 
     // Handle the error. 
    } 
    NSLog(@"%@", error); 


    [eventsArray addObject:routine]; 

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[eventsArray count] inSection:0]; 

    [self.routineTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

    [self.routineTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES]; 

    } 

감사가있을 수 있습니다

관련 문제