在ABPeoplePickerNavigationController中的删除功能上添加滑动

时间:2013-08-05 05:43:40

标签: ios objective-c abaddressbook

我正在使用Apple的地址簿做一个简单的联系人应用程序,并且在他们的开发者网站中提供了我设法创建联系人应用程序。但我在删除功能中遇到问题。我添加了删除代码

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

并显示删除按钮,但是当我点击删除时它没有响应,但下次重新加载时联系人会被删除。我已经搜索过但无法找到适当的解决方案,似乎每个人都有同样的问题。

现在我计划在PeoplePickerNavigationController上添加一个关于删除功能的滑动。我找到了一个关于如何在PeoplePickerNavigationController上标记联系人的代码并且它有效。现在我试图调整代码以添加刷卡删除功能,但我无法实现它。你能帮我修改删除时刷卡的代码吗?这是我点击联系人时勾选标记的代码。我需要替换此事件以在删除时滑动。

- (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;
}

这是我创建的函数,当我点击删除按钮时应该调用它,但只要它有效就可以添加你自己的删除功能。

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

}

2 个答案:

答案 0 :(得分:1)

我无法在删除时添加滑动但暂时我设法添加点击删除和警报视图。如果您可以在此代码中实现删除时刷卡的代码,请发布您的答案,我会将其标记为最佳答案,但是现在这是我得到的最佳答案。

-(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);
    }

}

//用于显示删除的警报视图选项

-(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];



    }
}

答案 1 :(得分:0)

要允许滑动到删除,您需要一些TableView数据源方法:

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

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

您需要实施- tableView:commitEditingStyle:forRowAtIndexPath:并检查是否editingStyle == UITableViewCellEditingStyleDelete,然后在那里进行删除。