【发布时间】:2011-11-23 09:51:48
【问题描述】:
我已经使用 JSONKit 从 url 获取 Json 到 initWithNibName 中的 NSDictionary
NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:jsonUrl]];
JSONDecoder *jsonKitDecoder = [JSONDecoder decoder];
jsonDic = [jsonKitDecoder parseJSONData:jsonData]; // NSDictionary *jsonDic
NSLog(@"Json Dictionary Fetched %@", jsonDic); // Display the Json fine :-)
NSLog(@"Dictionary Count %i", [jsonDic count]); // Display the Dic count fine :-)
array = [jsonDic objectForKey:@"courses"]; // NSArray *array
NSLog(@"Courses Found %@", array); // Display the array fine :-)
NSLog(@"Courses Count %i", [array count]);
这是 Json
{ "name":"Name 1" , "courses": [
{ "title":"Course 1" , "desc":"This is course 1" },
{ "title":"Course 2" , "desc":"This is course 2" },
{ "title":"Course 3" , "desc":"This is course 3" } ]
}
我将一个 tableview 拖到 xib。在 Interface Builder 上的 Connections 中将 IBOutlet UITableview tblView 设置为 tableview,并将 tableview dataSource 和委托给 FilesOwner
手动将tableview事件添加为
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
int cnt = [array count]; // CRASHES HERE Remeber returning 1; still why ?
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
int indx = [indexPath indexAtPosition:1];
/* THESE ARE ALL COMMENTED
NSString *key = [[jsonDic allKeys] objectAtIndex:indx];
NSString *value = [jsonDic objectForKey:key];
cell.textLabel.text = value;
/* / THESE ARE ALL COMMENTED
NSDictionary *eachItem = [array objectAtIndex:indx];
cell.textLabel.text = [eachItem objectForKey:@"title"];
// */
cell.textLabel.text = @"My Title"];
return cell;
}
请有人帮我解决这个问题。我需要在 tableview 上显示课程。
【问题讨论】:
-
这是 NSLog 中的内容:...经过一段时间... gdb-i386-apple-darwin(26422,0x778720) malloc: *** mmap(size=1420296192) 失败(错误代码=12) *** 错误:无法分配区域 *** 在 malloc_error_break 中设置断点以调试...经过一些... gdb 堆栈在内部错误点爬网:...经过一些... /SourceCache /gdb/gdb-967/src/gdb/utils.c:1144:内部错误:虚拟内存耗尽:无法分配 1420296192 字节。已检测到 GDB 内部的问题,进一步调试可能证明不可靠。调试器以状态 1 退出。调试器以状态 1 退出。
-
如果您取消删除有关数组洗牌的问题,我已经写了一个答案。
标签: json uitableview nsdictionary crash