2013-09-30 70 views
1

我試圖從AddressBook中檢索所有聯繫人,並將以下詳細信息存儲在可變數組中。UITableview始終爲所有單元格加載最後一個對象

的屬性是

@property (nonatomic, assign) ABAddressBookRef addressBook; 
@property (nonatomic, strong) NSMutableArray *contactList; 
@property (nonatomic, strong) IBOutlet UITableView *contactsTableView; 

方法來檢索所有聯繫人

- (void)getAllContacts { 
    //line moved inside For loop as per Amar's answer. 
    //NSMutableDictionary *personModel = [[NSMutableDictionary alloc]initWithCapacity:0]; 
    self.addressBook = ABAddressBookCreateWithOptions(NULL, NULL); //iOS 6 and above 
    CFArrayRef cList = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(self.addressBook, NULL, kABPersonSortByFirstName); 
    CFIndex nPeople = ABAddressBookGetPersonCount(self.addressBook); 
    for (int i=0; i<nPeople; i++) { 
     //Moving this line here per Amar's answer below. The code works perfectly now. 
     NSMutableDictionary *personModel = [[NSMutableDictionary alloc]initWithCapacity:0]; 
     ABRecordRef personRef = CFArrayGetValueAtIndex(cList, i); // Person will have name, phone number, address, email id and contact image 

     //Get the name 
     NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(personRef, kABPersonFirstNameProperty)); 
     NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(personRef, kABPersonLastNameProperty)); 
     NSString *name = nil; 
     if(firstName!=nil && lastName!=nil) { //both names are available 
      name = [NSString stringWithFormat:@"%@ %@",firstName,lastName]; 
     } else if(firstName!=nil && lastName==nil) { //last name not available 
      name = [NSString stringWithFormat:@"%@",firstName]; 
     } else if(firstName==nil && lastName!=nil) { //first name not available 
      name = [NSString stringWithFormat:@"%@",lastName]; 
     } else { 
      name = @"Unnamed Contact"; //both names not available 
     } 
     //Get the phone numbers 
     ABMultiValueRef phoneRef = ABRecordCopyValue(personRef, kABPersonPhoneProperty); 
     NSMutableArray *phoneNumbers = [NSMutableArray new]; 
     CFIndex ctr = ABMultiValueGetCount(phoneRef); 
     if(ctr!=0) { 
      NSString *phoneNumber = nil; 
      for (CFIndex i=0; i<ctr; i++) { 
       phoneNumber = (__bridge NSString *) ABMultiValueCopyValueAtIndex(phoneRef, i); 
       [phoneNumbers addObject:phoneNumber]; 
      } 
     } else { 
      [phoneNumbers addObject:@"Phone not available"]; 
     } 

     //Get the contact address 
     ABMultiValueRef addrRef = ABRecordCopyValue(personRef, kABPersonAddressProperty); 
     NSMutableArray *addresses = [NSMutableArray new]; 
     ctr = ABMultiValueGetCount(addrRef); 
     if(ABMultiValueGetCount(addrRef)!=0) { 
      for(CFIndex i=0; i<ABMultiValueGetCount(addrRef); i++) { 
       CFDictionaryRef addr = ABMultiValueCopyValueAtIndex(addrRef, i); 
       NSString *street = (__bridge NSString *)CFDictionaryGetValue(addr, kABPersonAddressStreetKey); 
       NSString *city = (__bridge NSString *)CFDictionaryGetValue(addr, kABPersonAddressCityKey); 
       NSString *state = (__bridge NSString *)CFDictionaryGetValue(addr, kABPersonAddressStateKey); 
       NSString *zip = (__bridge NSString *)CFDictionaryGetValue(addr, kABPersonAddressZIPKey); 
       NSString *address = [NSString stringWithFormat:@"%@, %@, %@ %@",street,city,state,zip]; 
       [addresses addObject:address]; 
      } 
     } else { 
      [addresses addObject:@"Address not available"]; 
     } 

     //Get the email address 
     ABMultiValueRef emailRef = ABRecordCopyValue(personRef, kABPersonEmailProperty); 
     NSMutableArray *emailAddresses = [NSMutableArray new]; 
     ctr = ABMultiValueGetCount(emailRef); 
     if(ctr!=0) { 
      for(CFIndex i=0; i<ctr; i++) { 
       NSString *eId = (__bridge NSString*)ABMultiValueCopyValueAtIndex(emailRef, i); 
       [emailAddresses addObject:eId]; 
      } 
     } else { 
      [emailAddresses addObject:@"EmailID not available"]; 
     } 

     //Get the contact image 
     UIImage *image = nil; 
     if(ABPersonHasImageData(personRef)) image = (__bridge UIImage *)(ABPersonCopyImageDataWithFormat(personRef, kABPersonImageFormatThumbnail)); 

     //Append the values to a dictionary 
     [personModel setValue:name forKey:@"cName"]; 
     [personModel setValue:phoneNumbers forKey:@"cPhone"]; 
     [personModel setValue:addresses forKey:@"cAddresses"]; 
     [personModel setValue:emailAddresses forKey:@"cEmailID"]; 
     [personModel setValue:image forKey:@"cImage"]; 

     [self.contactList addObject: personModel]; 
    } 
} 

在的tableView的數據源方法的cellForRowAtIndexPath

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ContactsCell forIndexPath:indexPath]; 

    cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:16]; 
    cell.textLabel.textColor = [UIColor blackColor]; 
    cell.detailTextLabel.font = [UIFont systemFontOfSize:14.0]; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    NSDictionary *model = [self.contactList objectAtIndex:indexPath.row]; 
    NSLog(@"Name:%@",[model valueForKey:@"cName"]); 
    cell.textLabel.text =[model valueForKey:@"cName"]; 
    return cell; 
} 

有我的地址簿四個觸點。但是,我的tableView總是返回最後一個聯繫人的名字(這是「無名聯繫人」,因爲它沒有名/姓)。

Unnamed Contact 
Unnamed Contact 
Unnamed Contact 
Unnamed Contact 

任何想法爲什麼?

回答

2

這是因爲這條線

NSMutableDictionary *personModel = [[NSMutableDictionary alloc]initWithCapacity:0]; 

for -loop之外。在迭代聯繫人列表並修改相同字典之前,您只需創建一次字典。因此它會一直存儲最後的聯繫信息。

取而代之,將上面的代碼行移入for循環中,它將創建一個新的字典,用於在列表中存儲每個聯繫人。

希望有幫助!

+1

這樣做。我真是個白癡。一旦SO允許我這樣做,我會接受這個答案。 – thandasoru

+1

沒問題,每個人都會犯錯誤! ;) – Amar

+1

的確如此。我想自己找出來,但太快了。也許我應該更努力地嘗試。無論如何感謝您的快速回答! – thandasoru

0

每次你存儲你的數據在同一個密鑰 'CNAME' 字典時間 「 [personModel的setValue:名稱forKey:@」 CNAME 「];」這就是爲什麼每次 值是覆蓋在 字典這就是爲什麼這個問題被提出相同的密鑰和最後一個記錄是商店,你需要 存儲你的數據有不同的密鑰,或者直接從陣列獲取數據而不是 字典

相關問題