当我从我的tableview中选择一些选定的用户时显示(Null)

时间:2015-01-21 05:36:32

标签: ios iphone

当我从我的tableview中选择一些选定的用户时,它显示(Null)。

-(void)retriveContactsFromAddressBook
{
    //CFErrorRef error = NULL;
     ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
    //self.phoneNumber=[NSMutableArray arrayWithObject:@" "];

    if (addressBook!=nil)
    {
        NSArray *allContacts=(__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
        NSUInteger i=0;
        for ( i = 0; i<[allContacts count]; i++)
        {
            ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
             NSString *firstName = (__bridge_transfer NSString*)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
            NSString *lastName=(__bridge_transfer NSString*)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);

            if (i==0) {
                self.FirstName=[NSMutableArray arrayWithObject:firstName];
                if (lastName==nil) {
                    self.LastName=[NSMutableArray arrayWithObject:@""];
                } else {
                    self.LastName=[NSMutableArray arrayWithObject:lastName];
                }

            } else {
                [self.FirstName addObject:firstName];

                if (lastName==nil) {
                    [self.LastName addObject:@""];
                } else {
                     [self.LastName addObject:lastName];
                }

            }
            NSString *full=[NSString stringWithFormat:@"%@ %@",[self.FirstName objectAtIndex:i],[self.LastName objectAtIndex:i]];

            if (i==0) {
                self.fullName=[NSMutableArray arrayWithObject:full];
            } else {
                [self.fullName addObject:full];
            }


        }



    }
    else
    {
        NSLog(@"Error Address book empty");
    }

}


 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
        tableViewCell.accessoryType=UITableViewCellAccessoryCheckmark;
        tableViewCell.accessoryView.hidden = YES;
         self.selectedUsers=[[NSMutableArray alloc]initWithObjects:self.phoneNumber[indexPath.row],nil];
        NSLog(@"selected users:%@",self.phoneNumber);

错误显示Bellow:

selected users:(null)

2 个答案:

答案 0 :(得分:0)

NSLog(@"selected users:%@",self.phoneNumber);应该记录nil,因为self.phoneNumber未分配并使用任何对象进行初始化。

答案 1 :(得分:0)

-(void)retriveContactsFromAddressBook
{
    //CFErrorRef error = NULL;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
    self.phoneNumber=[NSMutableArray array];
    self.FirstName=[NSMutableArray array];
    self.fullName=[NSMutableArray array];
    self.LastName=[NSMutableArray array];
if (addressBook!=nil)
{
    NSArray *allContacts=(__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
    NSUInteger i=0;
    for ( i = 0; i<[allContacts count]; i++)
    {
        ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];

        ABMultiValueRef phones =(__bridge ABMultiValueRef)((__bridge NSString*)ABRecordCopyValue(contactPerson, kABPersonPhoneProperty));

        if (ABMultiValueGetCount(phones) == 0)
            break;

        NSString* mobileLabel;

        for(CFIndex i = 0; i < ABMultiValueGetCount(phones); i++)
        {
            mobileLabel = (__bridge NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
            if ([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
            {
                NSString *phoneNumber = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(phones, i);
                if(phoneNumber.length > 0) {
                    [self.phoneNumber addObject:phoneNumber];
                    break;
                }
            }
        }

        NSString *firstName = (__bridge_transfer NSString*)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
        NSString *lastName=(__bridge_transfer NSString*)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);

        [self.FirstName addObject:firstName];

        if (lastName==nil) {
            [self.LastName addObject:@""];
        } else {
            [self.LastName addObject:lastName];
        }

        NSString *full=[NSString stringWithFormat:@"%@ %@",[self.FirstName objectAtIndex:i],[self.LastName objectAtIndex:i]];

        [self.fullName addObject:full];
    }
}
else
{
    NSLog(@"Error Address book empty");
}

}

viewDidLoad

中初始化self.selectedUsers = [NSMutableArray array];
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
        if([MFMessageComposeViewController canSendText])
        {
            picker.messageComposeDelegate = self;
            picker.recipients = @[self.phoneNumber[indexPath.row]];
            [self presentViewController:picker animated:YES completion:nil];
        }
    }

我更新了我的代码..

包含支持消息的MessageUI框架