2012-09-02 25 views
2

我有一個屬性的NSMutableArray在我的視圖控制器NSMutableArray的部分空後回憶

ContactsViewController.h:

@interface ContactsViewController : UIViewController <UITableViewDelegate,  UITableViewDataSource> 

@property (nonatomic,strong) NSMutableArray *contacts; 
... 
@end 

在這個視圖控制器我填我的陣列上 「的viewDidLoad」

ContactsViewController.m:

@implementation ContactsViewController 

@synthesize contacts; 
... 
... 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    DBhandler *handler = [[DBhandler alloc] init]; 

    if (contacts) 
    [contacts removeAllObjects]; 
    else 
    contacts = [[NSMutableArray alloc] init];  
    // Get all my contacts that are in my core data file 
    // This function returns a NSMutableArray 
    contacts=[handler getContacts]; 

//loop through contacts of addressbook when user wants that 
if ([allContactSwitch isOn]) 
{ 

    //open link to addressbook 
    ABAddressBookRef addressBook = ABAddressBookCreate(); 
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook); 
    CFIndex nPeople = ABAddressBookGetPersonCount(addressBook); 

    for(CFIndex personIndex = 0; personIndex < nPeople; personIndex++) {    
     ABRecordRef refVal = CFArrayGetValueAtIndex(allPeople, personIndex);    
     Boolean newContact = true; 
     // check if contact is already in Core data File 
     for(CFIndex i = 0; i < [contacts count]; i++) { 
      contact *checkcontact=[contacts objectAtIndex:i]; 
      if (personIndex==checkcontact.personRef) 
       newContact = FALSE; 
     } 
     if (newContact) 
     { 
      contact *dummycontact = [[contact alloc]init]; 
      dummycontact.personRef = personIndex; 
      dummycontact.contactName = (__bridge NSString *)(ABRecordCopyCompositeName(refVal)); 
      // Add contact to array 
      [contacts addObject:dummycontact]; 
     } 
    } 
} 
// Just to check, the entire array looks fine! 
for(CFIndex i = 0; i < [contacts count]; i++) { 
    contact *dummycontact=[contacts objectAtIndex:i]; 
    NSLog(@"Name after build: %@", dummycontact.contactName); 
} 

} 

但後來當差異爲表視圖NT細胞填充,從傳來的NSMutableArray的一部分[處理getContacts]是空的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
//NSLog(@"cell number %i",indexPath.row); 
static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) {   
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault 
            reuseIdentifier:CellIdentifier]; 
} 

// Set up the cell... 
contact *dummycontact=[contacts objectAtIndex:indexPath.row]; 
// Only part of the objects in the array contacts contain data! 
NSLog(@"Name cell: %@ %i", dummycontact.contactName, indexPath.row); 

cell.textLabel.text = dummycontact.contactName; 

return cell; 
} 

這可能是與事實,在創建的對象的內存[處理getContacts做]在此期間被清除。但我不知道如何解決這個問題。我試過克隆或複製[handle get contacts]的輸出,但我沒有成功。 要完成的功能「getContacts」:

-(NSMutableArray*)getContacts{ 

NSMutableArray *contacts = [[NSMutableArray alloc] init]; 

NSManagedObjectContext *context = [self managedObjectContext]; 
DBcontact *contact = [NSEntityDescription 
         insertNewObjectForEntityForName:@"WhappContacts" 
         inManagedObjectContext:context]; 
// Test listing all FailedBankInfos from the store 
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"WhappContacts" 
              inManagedObjectContext:context]; 
[fetchRequest setEntity:entity]; 
NSError *error; 
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error]; 
for (contact in fetchedObjects) { 
    [contacts addObject:contact]; 
    // Data seems fine here. 
    NSLog(@"Name in: %@", contact.contactName); 
} 
return contacts; 
} 

任何幫助將是非常讚賞!

+0

您是否使用ARC或這是一個弧前的代碼? – dasblinkenlight

+0

是的,我在我的項目中使用ARC – Danielvd

回答

0

另一種方法是放置在聯繫人信息中的字典:

- (NSMutableArray*)getContacts { 
    NSMutableArray *contacts = [[NSMutableArray alloc] init]; 

    NSManagedObjectContext *context = [self managedObjectContext]; 
    DBcontact *contact = [NSEntityDescription insertNewObjectForEntityForName:@"WhappContacts" inManagedObjectContext:context]; 
    // Test listing all FailedBankInfos from the store 
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"WhappContacts" inManagedObjectContext:context]; 
    [fetchRequest setEntity:entity]; 
    NSError *error; 
    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error]; 
    for (contact in fetchedObjects) { 
     NSDictionary *contactDict = [NSDictionary dictionaryWithObjectsAndKeys: 
           contact.contactName, @"contactName", nil]; //add other necessary contact information 

     [contacts addObject:contactDict]; 
     // Data seems fine here. 
     NSLog(@"Name in: %@", contact.contactName); 
    } 

    return contacts; 
} 

並獲取信息:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    //NSLog(@"cell number %i",indexPath.row); 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) {   
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // Set up the cell... 
    NSDictionary *dummycontact = [contacts objectAtIndex:indexPath.row]; 
    // Only part of the objects in the array contacts contain data! 
    NSLog(@"Name cell: %@ %i", [dummycontact objectForKey:@"contactName"], indexPath.row); 

    cell.textLabel.text = [dummycontact objectForKey:@"contactName"]; 

    return cell; 
}