0
 - (void)viewDidLoad
{
  [super viewDidLoad];

  if (self.document.documentState == UIDocumentStateClosed){
    [self.document openWithCompletionHandler:^(BOOL success) {

        self.list = [[Categories getArrayForFirstTable:self.document.managedObjectContext] mutableCopy];

    }];

 }
 [self.tableView reloadData];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

// Return the number of rows in the section.
return [self.list count];
}

numberOfRowsInSection は、openWithCompletionHandler のブロックの前に呼び出されます。
[self.list count] は nil です。なぜですか?

4

1 に答える 1

1

openWithCompletionHandlerアップルのドキュメントによると、ブロックは非同期操作です

このメソッドを呼び出して、ドキュメントを非同期で開いて読み取る一連のメソッド呼び出しを開始します。このメソッドは、fileURL プロパティからドキュメントのファイル システムの場所を取得します。open 操作が完了すると、completedHandler のコードが実行されます。

したがって、意味のある結果を得る前に、[self.tableView reloadData]実行され、トリガーされます-tableView:numberOfRowsInSection:self.listself.list = [[Categories getArrayForFirstTable:self.document.managedObjectContext] mutableCopy];

このコードはあなたのニーズを満たすかもしれません:

[self.document openWithCompletionHandler:^(BOOL success) {
    self.list = [[Categories getArrayForFirstTable:self.document.managedObjectContext] mutableCopy]; 
    // At this point(no matter when), self.list is returned and you can use it.
    [self.tableView reloadData];
}];
于 2013-08-17T11:05:18.767 に答える