次のように定義された ViewController があります。
@interface SectionController : UITableViewController {
NSMutableArray *sections;
}
- (void) LoadSections;
LoadSection が呼び出されると、NSURLConnection が呼び出されて URL がロードされ、次にそれが呼び出されます。
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[connection release];
[responseData release];
NSDictionary *results = [responseString JSONValue];
NSMutableArray *jSections = [results objectForKey:@"Items"];
sections = [NSMutableArray array];
for (NSArray* jSection in jSections)
{
Section* section = [Section alloc];
section.Id = [jSection objectForKey:@"Id"];
section.Description = [jSection objectForKey:@"Description"];
section.Image = [jSection objectForKey:@"Image"];
section.Parent = [jSection objectForKey:@"Parent"];
section.ProductCount = [jSection objectForKey:@"ProductCount"];
[sections addObject:section];
[section release];
}
[jSections release];
[results release];
[delegate sectionsLoaded];
[self.view reloadData];
}
データは正しく解析され、多くの項目で満たされたセクションができました。
[self.view reloadData] を呼び出すと、セルにデータを表示するデリゲート メソッド cellForRowAtIndexPath へのコールバックが強制されますが、この時点でセクションは再び nil になります。
誰かが私の間違いを指摘できますか?私は客観的なcの初心者であり、おそらくポインターの問題であることを認めなければなりません。必要なことは、reloadData を呼び出した後にセクションの値を保持することです。
どうもありがとう。