0
votes

I have a problem with populating the data from NSMutableArray to UITableView. In the ViewDidLoad, I did a network call which gets data from Parse and return an NSMutableArray called 'journalEntries' then I copied the data in this array to the NSMutableArray variable called 'allEntries'. I set a breakpoint here and verified that _allEntries has 4 objects (not nil). However, when it comes to numberOfRowsInSection method, _allEntries.count returns 4 but I set a breakpoint here and all the objects in _allEntries becomes nil.

- (void)viewDidLoad {
    [super viewDidLoad];

    _allEntries = [[NSMutableArray alloc] init];

    [MMDatabaseHelper getAllJournalEntries:^(NSMutableArray *journalEntries) {

        for (MMJournalEntry *entry in journalEntries)
             [_allEntries addObject:entry];
        // I set a breakpoint here and verified that _allEntries has 4 objects

        [self.tableView reloadData];
    }];

This method below returns 4 but all the objects in the allEntries array are nil. They weren't nil in the viewDidLoad.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return _allEntries.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    MMJournalEntry *currentEntry = _allEntries[indexPath.row];

    return cell;
}

Edit 1: The first screenshot is when the network call is done. The second is when it comes to the numberOfRowsInSection method

Pic1

Pic2

Edit 2: This is getAllJournalEntries method

+(void) getAllJournalEntries:(void(^)(NSMutableArray *journalEntries))callback {
    PFQuery *query = [PFQuery queryWithClassName:JournalTable];
    [query findObjectsInBackgroundWithBlock:^(NSArray *entries, NSError *error) {
        if(entries != nil && entries.count > 0) {
            NSMutableArray *mainEntries = [[NSMutableArray alloc] init];
            for (PFObject *entry in entries) {
                //convert to journal entry...
                MMJournalEntry *je = [[MMJournalEntry alloc] init];
                je.objId = entry.objectId;
                je.textContent = entry[@"textContent"];
                je.createdByUserId = entry[@"createdByUserId"];
                je.cityStateName = entry[@"cityStateName"];
                je.lattitude = entry[@"lattitude"];
                je.longitude = entry[@"longitude"];
                je.lattitude = entry[@"lattitude"];
                je.tags = [NSMutableArray arrayWithArray:[entry[@"tags"] componentsSeparatedByString:@","]];
                je.numberOfHearts = entry[@"numberOfHearts"];

                [mainEntries addObject:je];
            }
            callback(mainEntries);
        }
        else
            callback(nil);
    }];
}
3
Elements of an NSArray (or NSMutableArray) cannot be nil. If you are seeing the "objects" in _allEntries as nil, there is something wrong with your debugger or your interpretation of its output. - Ian MacDonald
Where's the declaration for _allEntries? If it's a @property of the viewController, maybe try self.allEntries instead? Shouldn't access the backing ivar directly anyway. Also if you're trying to access the entry for row #10 for example but you only have 5 entries, there is no value for that array index. Would be a good idea to confirm that entry exists anyway. - mc01
In cellForRow... NSLog your _allEntries object. It's probably nil (will log as (null)). It's illegal for there to be nil entries in an NSArray. - Hot Licks
What queue does getAllJournalEntries: call its completion handler on? It's easy to imagine that reloadData wouldn't work from a background queue. In that case your assumption that -tableView:numberOfRowsInSection: is called after -viewDidLoad wouldn't necessarily stand up. - Tommy
Can you show the definition of your MMJournalEntry object - in particular its property declarations - it looks like they are being released - Paulw11

3 Answers

1
votes

As you receive an NSMutableArray from Parse, why you don't simply copy it on an NSArray? You should redeclare your allEntries property and you don't need the forin loop.

_allEntries = journalEntries.copy;

As NSArray is not mutable, it will never lose values.

1
votes

After spending a lot of time trying to fix this bug, I finally fixed it. Thanks to Paulw11's comment, the problem is with MMJournalEntry. All of the properites in this class were declared weak instead of strong. My teammate wrote this class so I didn't check. Thanks everyone.

-1
votes

If there is no alloced cell that use @"Cell" as Indentifier.

The method:

[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

will return nil