Ios 加载UITableView时,NSMutableArray数据变为零

Ios 加载UITableView时,NSMutableArray数据变为零,ios,uitableview,nsmutablearray,Ios,Uitableview,Nsmutablearray,我在将数据从NSMutableArray填充到UITableView时遇到问题。在viewdiload中,我进行了一个网络调用,从Parse中获取数据,并返回一个名为“journalEntries”的NSMutableArray”变量,然后我将该数组中的数据复制到名为“allEntries”的NSMutableArray变量中。我在这里设置了一个断点,并验证了\u allEntries有4个对象(不是nil)。但是,当涉及到numberOfRowsInSection方法时,\u allEntri

我在将数据从NSMutableArray填充到UITableView时遇到问题。在
viewdiload
中,我进行了一个网络调用,从Parse中获取数据,并返回一个名为“
journalEntries
”的
NSMutableArray
”变量,然后我将该数组中的数据复制到名为“
allEntries
”的
NSMutableArray
变量中。我在这里设置了一个断点,并验证了
\u allEntries
有4个对象(不是nil)。但是,当涉及到
numberOfRowsInSection
方法时,
\u allEntries.count
返回4,但我在这里设置了一个断点,
\u allEntries
中的所有对象都变为
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];
    }];
下面的这个方法返回4,但是allEntries数组中的所有对象都是nil。他们不是零

- (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;
}
编辑1:第一个屏幕截图是网络呼叫完成时。第二个是当涉及到
numberofrowsinssection
方法时

+(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);
    }];
}

编辑2:这是
getAllJournalEntries
方法

+(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);
    }];
}

如果没有使用@“cell”作为标识符的alloced单元格

方法:

[tableView dequeueReusableCellWithIdentifier:@“Cell”forIndexPath:indexPath]


当您从Parse接收到一个
NSMutableArray
时,将返回
nil
,为什么不简单地将它复制到
NSArray
?您应该重新声明allEntries属性,而不需要
forin
循环

_allEntries = journalEntries.copy;

由于
NSArray
是不可变的,所以它永远不会丢失值。

在花了很多时间试图修复这个bug之后,我终于修复了它。多亏了Paulw11的评论,问题在于
MMJournalEntry
。该类中的所有属性均声明为
,而不是
。我的队友写了这门课,所以我没有检查。谢谢大家。

一个
NSArray
(或
NSMutableArray
)的元素不能是
nil
。如果将
\u allEntries
中的“对象”视为
nil
,则调试器或对其输出的解释有问题。
\u allEntries
的声明在哪里?如果它是viewController的@属性,可以尝试使用
self.allEntries
?无论如何都不应该直接访问支持ivar。另外,如果您试图访问第10行的条目,但只有5个条目,那么该数组索引没有值。确认条目是否存在是个好主意。在cellForRow中。。。NSLog您的_allEntries对象。它可能为零(将记录为
(null)
)。NSArray中没有条目是非法的。
getAllJournalEntries:
调用其完成处理程序的队列是什么?很容易想象,
reloadData
在后台队列中不起作用。在这种情况下,假设在
-viewdiload
之后调用
-tableView:numberofrowsinssection:
,就不一定成立了。您能否显示
MMJournalEntry
对象的定义,特别是它的属性声明?看起来它们就像是从iOS6发布的一样,您不再需要担心此-如果不存在单元格,则会为您创建它。。。。实际上,这就是
dequeuereusablecellwithindicator:
的类形式与
dequeuereusablecellwithindicator:forindexath:
的现代形式之间的区别。后者保证它将返回一个单元格(并且,可能缓存更好,因为它有更多的信息)。@mc01。好啊我真的不知道。。或者当调用CellForRowatineXpath时,
journalEntries
被释放……但我记得NSMutableArray的
addObject
保留了该对象。