0

I am doing a simple contacts app using Apple's addressbook and as provided in their developer site I have managed to create the contact app. But I have problem in the delete function. I have added the code for deletion

[picker setValue:[NSNumber numberWithBool:YES] forKey:@"allowsDeletion"];

and the delete button appears, but when I click on delete it doesnt respond but the contact does get deleted when I reload next time. I have searched through but couldnt find a proper solution for this, seems like everyone has same issue.

Now I am planning to add a swipe on delete function on the PeoplePickerNavigationController. I have found a code on how to mark the contact on PeoplePickerNavigationController and it works. Now I am trying to tweak the code to add the swipe on delete function but I am having trouble implementing it. Can you help me modify the code for swipe on delete? This is the code that puts a check mark when I click on a contact. I need to replace this event to swipe on delete.

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person{
UIView *view = peoplePicker.topViewController.view;
UITableView *tableView = nil;
for(UIView *uv in view.subviews)
{
    if([uv isKindOfClass:[UITableView class]])
    {
        tableView = (UITableView*)uv;
        break;
    }
}
if(tableView != nil)
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:[tableView indexPathForSelectedRow]];
    if(cell.accessoryType == UITableViewCellAccessoryNone){
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }        
    [cell setSelected:NO animated:YES];
}
return NO;
}

This is the function I created that should be called when I click on delete button, but you can add your own delete function as long as it works.

-(void)deleteContct
{
    ABAddressBookRef addressBook= ABAddressBookCreate();
    ABAddressBookRemoveRecord(addressBook, (ABRecordRef)person,NULL);
    ABAddressBookSave(addressBook, NULL);

}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Francis F
  • 3,157
  • 3
  • 41
  • 79

2 Answers2

1

I wasnt able to add swipe on delete but for time being I managed to add click on delete with alert view. If u can implement code for swipe on delete in this code do post your answer, I will mark that as best answer, but for now this is the best answer that I got.

-(BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person {

    person12=person;

    if(deleteFlag)
    {
    UIView *view = peoplePicker.topViewController.view;
    UITableView *tableView = nil;
    for(UIView *uv in view.subviews) 
    {
        if([uv isKindOfClass:[UITableView class]])
        {
            tableView = (UITableView*)uv;
            break;
        }
    }

        //[tableView setEditing:YES]; // I was trying to write code for this delete function, for swipe delete
    if(tableView != nil)
    {



        [self tableView:tableView commitEditingStyle:UITableViewCellEditingStyleDelete forRowAtIndexPath:[tableView indexPathForSelectedRow]];

    }
    return NO;
    }
    else
    {
            [peoplePicker dismissModalViewControllerAnimated:NO];

            ABPersonViewController *picker = [[ABPersonViewController alloc] init];
            picker.personViewDelegate = self;
            picker.displayedPerson = person;

// Allow users to edit the person’s information
            picker.allowsEditing = YES;


            [self.navigationController pushViewController:picker animated:YES];

            return YES;
    }
}

//Deletes contact here

-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //NSLog(@"%@",[alertView buttonTitleAtIndex:buttonIndex]);
    if([alertView buttonTitleAtIndex:buttonIndex]==@"Delete") // This is where my click on delete works
    {
            ABAddressBookRef addressBook= ABAddressBookCreate();
            ABAddressBookRemoveRecord(addressBook, (ABRecordRef)person12,NULL);
            ABAddressBookSave(addressBook, NULL);
            CFRelease(addressBook);

    }
    else if([alertView buttonTitleAtIndex:buttonIndex]==@"Delete All") // This is for a different menu option for deleting all contact at once
    {
        ABAddressBookRef addressBook= ABAddressBookCreate();
        CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
        for (CFIndex i = 0; i < CFArrayGetCount(people); i++)
        {
            self.person12 = CFArrayGetValueAtIndex(people, i);
            ABAddressBookRemoveRecord(addressBook, self.person12,NULL);
        }
        CFBridgingRelease(people);
        ABAddressBookSave(addressBook,NULL);
        [self showContacts];
        CFRelease(addressBook);
    }

}

//For displaying alert view options for delete

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *str=[[NSString stringWithFormat:@"%@", (__bridge_transfer NSString *)ABRecordCopyValue(self.person12, kABPersonFirstNameProperty)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

    NSString *delMsg = [NSString stringWithFormat:@"Delete Contact %@",str];

    if(editingStyle==UITableViewCellEditingStyleDelete)
    {
        UIAlertView *message= [[UIAlertView alloc] initWithTitle:delMsg message:@"This action can't be undone, are you sure?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Delete", nil];


        [message show];



    }
}
Francis F
  • 3,157
  • 3
  • 41
  • 79
0

To allow for swipe-to-delete, you need a few TableView datasource methods:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  return UITableViewCellEditingStyleDelete;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  return YES;
}

And you need to implement - tableView:commitEditingStyle:forRowAtIndexPath: and check if editingStyle == UITableViewCellEditingStyleDelete and then do your deletion there.

garrettmurray
  • 3,338
  • 1
  • 25
  • 23
  • In my xib, I have a window, navigation controller and a table inside navigation controller. When I run the program the initial screen is shown on a table with, display and create contact. When I click on display it calls the ABPeoplePickerNavigationController and shows the contact. I cant access the table view inside this directly, I know the methods which you have mentioned, if I call them it will only affect to my initial menu screen and not the one inside the navigation controller, thats why here its access through with subviews. – Francis F Aug 06 '13 at 04:04
  • If I call [tableView setEditing:YES]; It shows the delete button when swiped, but when clicked it doesnt respond. When I checked it doesnt call the methods which you have mentioned. These methods respond only for the initial menu screen and not inside the navigation controller. So is there any way where I can code the action for the delete button which appears on swipe? – Francis F Aug 06 '13 at 04:05