2013-08-05 23 views
0

我正在做一個簡單的聯繫人應用程序,使用蘋果的地址簿,並在他們的開發人員站點中提供我已經設法創建聯繫人應用程序。但我在刪除功能有問題。我添加的代碼刪除在ABPeoplePickerNavigationController中添加刪除功能滑動

[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); 

} 

回答

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][email protected]"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][email protected]"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]; 



    } 
} 
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,然後做你的缺失存在。

+0

顯示警報視圖選項在我的廈門國際銀行,我有一個窗口,導航控制器和導航控制器內的表。當我運行程序時,初始屏幕顯示在桌面上,顯示並創建聯繫人。當我點擊顯示時,它會調用ABPeoplePickerNavigationController並顯示聯繫人。我不能直接訪問裏面的表格視圖,我知道你提到的方法,如果我打電話給他們,它只會影響到我的初始菜單屏幕,而不是導航控制器內的那個,這就是爲什麼在這裏訪問通過子視圖。 – Gamerlegend

+0

如果我調用[tableView setEditing:YES];它顯示刪除按鈕時刷,但點擊它不響應。當我檢查它沒有調用你提到的方法。這些方法僅對初始菜單屏幕響應,不在導航控制器內。那麼有沒有什麼辦法可以在刷卡時出現刪除按鈕的操作? – Gamerlegend

相關問題