0

我正在使用 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);

}
4

2 回答 2

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



    }
}
于 2013-08-12T05:52:27.407 回答
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然后在那里进行删除。

于 2013-08-05T22:51:34.880 回答