我要取的Json與JSONKit從網址到NSDictionary中在initWithNibNameUITableview崩潰在numberOfRowsInSection; JSON中的NSDictionary到UITableView的
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到廈門國際銀行。設置IBOutlet中的UITableView tblView到在界面生成器連接的實現代碼如下,並ASLO的的實現代碼如下數據源和委託FilesOwner
手動添加的實現代碼如下事件
- (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)failed(error code = 12) ***錯誤:無法分配區域 ***在malloc_error_break中設置斷點以調試 ...之後... gdb堆棧在內部錯誤點處抓取: ...之後... /SourceCache/gdb/gdb-967/src/gdb/utils.c:1144:internal-error:虛擬內存耗盡:無法分配1420296192個字節。 GDB的內部問題已被檢測到, 進一步的調試可能證明不可靠。 調試器已退出狀態1.The Debugger已退出狀態1. –
如果您取消刪除有關陣列隨機播放的問題,我已經寫了一個答案。 – C5H8NNaO4