2014-10-04 57 views
-1

我正在開發的聯繫人列表中的應用程序,這樣我需要把聯繫人數據,並存儲在tableview中我做了如下的iOS:保存在數組字符串數據

 - (void)listPeopleInAddressBook:(ABAddressBookRef)addressBook 
    { 
    NSInteger numberOfPeople = ABAddressBookGetPersonCount(addressBook); 
    contactList= CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook)); 

for (int i = 0; i < numberOfPeople; i++) 
{ 
    ABRecordRef person = (__bridge ABRecordRef)contactList[i]; 

    firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty)); 
lastName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty)); 
    NSLog(@"Name:%@ %@", firstName, lastName); 

    ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty); 

    CFIndex numberOfPhoneNumbers = ABMultiValueGetCount(phoneNumbers); 
    for (CFIndex i = 0; i < numberOfPhoneNumbers; i++) 
    { 
     NSString *phoneNumber = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, i)); 
     NSLog(@" phone is:%@", phoneNumber); 
    } 
     CFRelease(phoneNumbers); 

      } 


     } 

目標得到的字符串格式的數據是名字和姓氏。我的問題是我需要將名字和姓氏中存在的數據保存到數組但是該數組存在for循環的一側。以便我將數組數據傳遞給表視圖。

回答

0
- (void)listPeopleInAddressBook:(ABAddressBookRef)addressBook 
{ 

    NSInteger numberOfPeople = ABAddressBookGetPersonCount(addressBook); 
    NSMutableArray * contactList= CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook)); 

    NSMutableArray *tableviewArray =[[NSMutableArray alloc] init]; // ADD This Line 

    for (int i = 0; i < numberOfPeople; i++) 
    { 
     ABRecordRef person = (__bridge ABRecordRef)contactList[i]; 

     NSString * firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty)); 
     NSString * lastName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty)); 
     NSLog(@"Name:%@ %@", firstName, lastName); 




     ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty); 

     CFIndex numberOfPhoneNumbers = ABMultiValueGetCount(phoneNumbers); 
     for (CFIndex i = 0; i < numberOfPhoneNumbers; i++) 
     { 
      NSString *phoneNumber = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, i)); 
      NSLog(@" phone is:%@", phoneNumber); 
     } 

     [tableviewArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:firstName , @"first_name" , lastName , @"last_name" , nil]]; // ADD This Line 

     CFRelease(phoneNumbers); 

    } 


} 
+0

如u給的tableview陣列僅適用於循環,如果陣列出來的那個循環返回空值我需要保存的數據一樣當過它已被保存在不同的方法和什麼的電話號碼提前致謝,請幫助。 – 2014-10-04 05:35:24

+0

創建一個全球陣列 – 2014-10-04 05:36:22

+0

kk我得到了,但目標得到電話號碼爲最後的索引名稱只有它意味着它顯示las名稱的電話號碼只有不是所有請讓我知道 – 2014-10-04 05:44:10

相關問題