我有一个 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 进行回调,然后该方法应该将数据呈现到单元格中,但是此时节现在再次为零。
有人可以指出我的错误吗?我必须承认我是目标 c 的新手,这可能是一个指针问题。需要做的是在调用 reloadData 后保留部分的值。
非常感谢。