Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-09-2009, 06:40 PM   #1 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 15
Argus is on a distinguished road
Default Delete rows with CoreData and AlertView

I'm working on an app using CoreData that lets user add or delete items to a TableView. Before deleting an item, I'd like to give them the option to verify they really want to delete with an AlertView.

I have the AlertView setup here:

Code:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath {
	if (editingStyle == UITableViewCellEditingStyleDelete) {

		NSString *message = [[NSString alloc] initWithFormat:@"Do you really want to delete ?"];
		
		UIAlertView *alert = [[UIAlertView alloc] 
							  initWithTitle: @"Alert" 
							  message: message 
							  delegate: self
							  cancelButtonTitle: @"Cancel"
							  otherButtonTitles: @"OK", nil];
		
		[alert show];
		[alert release];
[message release];
That's working fine and the buttons are set up properly.

I've put the call to delete the object here:

Code:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

	if (buttonIndex == 1) {
	
		NSIndexPath *indexPath = [myTableView indexPathForSelectedRow];
	
		NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
		[context deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]];

		
		NSError *error;
		if (![context save:&error]) {
			// Update to handle the error appropriately.
			NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
			exit(-1);  // Fail
			
		}  
	}	
	
	else {
	}
}
I thought it was working fine because it deleted the item in the first row.

The problem is is that it always deletes the top row, no matter what row I've selected. i.e., if I've added 3 items (3 rows) to the table view, when I try to delete the 3rd row and hit the delete button, the top row disappears. When I hit delete again, the 2nd row (now the top row) disappears.

If someone could point me in the right direction on using an Alert View with CoreData I'd appreciate it. I couldn't find anything to help at the Developer web site.
Argus is offline   Reply With Quote
Old 12-13-2009, 11:52 AM   #2 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 15
Argus is on a distinguished road
Default

Bump. Anyone have any ideas on this one. Thanks.
Argus is offline   Reply With Quote
Old 09-14-2010, 02:40 PM   #3 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 12
gammapoint is on a distinguished road
Default

Quote:
Originally Posted by Argus View Post
Bump. Anyone have any ideas on this one. Thanks.
I had the same issue. I use an instance variable to store the index path when the user decides to delete (in my case mark the row inactive). The -inactive method is called when the user selects a row & hits the delete/inactive button. When the user selects a row a check is make to make sure the selected row is not already inactive. Note: The Vacation object has a BOOL attribute called "active". For a demo visit our website at www.gamma-point.com.

Here's the code:

In the .h file for the table view controller:

Code:
NSIndexPath * inactiveIndexPath;

}
@property (nonatomic, retain) NSIndexPath * inactiveIndexPath;
In the .m file for the table view controller:
Code:
@synthesize inactiveIndexPath;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger ints[2] = {[indexPath section],[indexPath row]};
    inactiveIndexPath = [[NSIndexPath alloc] initWithIndexes:ints length:2];
    NSLog(@"Selected ( %d, %d)", [inactiveIndexPath section], [inactiveIndexPath row]);
    NSManagedObject *aVacationDBobject = [self.fetchedResultsController objectAtIndexPath:inactiveIndexPath];
    if (![[aVacationDBobject valueForKey:@"active"] boolValue]) {
        [self.tableView deselectRowAtIndexPath:inactiveIndexPath animated:YES];
        [inactiveIndexPath release];
        inactiveIndexPath = nil;
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (inactiveIndexPath != nil) {
        [inactiveIndexPath release];
        inactiveIndexPath = nil;
        }

    
}
-(void) inactive {
    
    if (inactiveIndexPath == nil) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Select Row" 
                                                        message:@"Please select a vacation entry to mark it inactive." 
                                                       delegate:nil   cancelButtonTitle:@"OK"  otherButtonTitles:nil];
        [alert show];
        
        [alert release];
        return;
    } else {
        UIActionSheet *actionSheet = [[UIActionSheet alloc] 
                                      initWithTitle:@"Inactivate a Vacation entry" 
                                      delegate:self 
                                      cancelButtonTitle:@"No Way!"
                                      destructiveButtonTitle:@"Yes, I'm Sure!" 
                                      otherButtonTitles:nil];
        [actionSheet showInView:self.view];
        [actionSheet release]; 
    }
}


- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (inactiveIndexPath == nil) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Select Row" 
                                                        message:@"Please select a vacation entry to mark it inactive." 
                                                       delegate:nil   cancelButtonTitle:@"OK"  otherButtonTitles:nil];
        [alert show];
        
        [alert release];
        return;
    }
    
    
        
    [self.tableView deselectRowAtIndexPath:inactiveIndexPath animated:YES];
    
    
    if (buttonIndex == [actionSheet cancelButtonIndex]) return;
    
    //Find the event date
    NSManagedObject *aVacationDBobject = [self.fetchedResultsController objectAtIndexPath:inactiveIndexPath];
    NSString *dateForEvent = [aVacationDBobject valueForKey:@"date"];
    
    NSLog(@" DB Access for marking the vacation inactive for date %@", dateForEvent);
    //Find the vacation object in the DB
    ProCalendarAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    
    NSError *error;

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Vacation"     inManagedObjectContext:context];
    [request setEntity:entityDescription];
    
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"(date = %@)",dateForEvent];
    [request setPredicate:pred];
    NSManagedObject *aVacationMO = nil;
    NSArray *objects = [context executeFetchRequest:request error:&error];
    if (objects == nil){
        NSLog(@"There was an error!");
        // Do whatever error handling is appropriate
    }
    if ([objects count] > 0) {
        NSLog(@"Overriding the table entry for date %@", dateForEvent);
        aVacationMO = [objects objectAtIndex:0];
    }
    else{
        NSLog(@"Event not found");
        if (inactiveIndexPath != nil) {
            [inactiveIndexPath release];
            inactiveIndexPath = nil;
        }
        return;
    }
    
    //mark the active flag in DB to NO
    NSNumber *active = [NSNumber numberWithBool:NO];
    [aVacationMO setValue: active forKey:@"active"];
    
    //Cancel the alarm for this event
    NSData *data = [aVacationMO valueForKey:@"alarm"];
    UILocalNotification *alarm = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    
    AlarmUtil *alarmUtil = [[AlarmUtil alloc] init];
    [alarmUtil cancelAlarm:alarm];
    [alarmUtil release];
    
    //mark the event for this vacation entry as inactive or delete the event row??
    
    if (context != nil) {
        if ([context hasChanges] && ![context save:&error]) {
            /*
             Replace this implementation with code to handle the error appropriately.
             
             abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
             */
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            NSLog(@"Event not marked inactive");
            return;
        }
    }
    

    
    
    
    
    //reload the cell
    [self.tableView reloadRowsAtIndexPaths:[NSArray    arrayWithObject:inactiveIndexPath] withRowAnimation:UITableViewRowAnimationFade];

        [self displayTotals];
    
    if (inactiveIndexPath != nil) {
        [inactiveIndexPath release];
        inactiveIndexPath = nil;
    }
    

    
}

Last edited by gammapoint; 09-14-2010 at 02:45 PM.
gammapoint is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 330
13 members and 317 guests
akacaj, alexP, ClerurcifeDer, Duncan C, givensur, GraffitiCircus, guusleijsten, JmayLive, NetGuru, Paul Slocum, Punkjumper, Sloshmonster, yys
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,114
Posts: 402,883
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:47 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0