2

我的视图控制器中有一个属性 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);
}

}

但后来当表格视图的不同单元格被填充时,来自 [handle getContacts] 的 NSMutableArray 部分为空:

- (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;
}

这可能与同时清除 [handle 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;
}

任何帮助将不胜感激!

4

1 回答 1

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;
}
于 2013-08-21T07:23:49.983 回答