2011-11-23 35 views
0

我要取的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上顯示課程。

+0

這是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. –

+0

如果您取消刪除有關陣列隨機播放的問題,我已經寫了一個答案。 – C5H8NNaO4

回答

0

如果您向其發送消息(此處爲計數),那麼您的數組對象可能會解除分配導致崩潰(EXEC_BAD_ACCESS?)。從行

array = [jsonDic objectForKey:@"courses"]; // NSArray *array 

它似乎是autoreleased,你應該保留它。

編輯:

你可以保留它:

將其設置爲保留財產

@property(nonatomic, retain) NSArray* array; 

,並使用訪問

self.array = [jsonDic objectForKey:@"courses"]; 

自動釋放舊值並保留新的。或者通過保留它明確地

array = [[jsonDic objectForKey:@"courses"] retain]; 

那麼你一定不能忘記釋放它,當你做,否則你會泄漏內存。你應該閱讀Advanced Memory Management Programming Guide,它給了你一個徹底的解釋,並且瞭解基本技術是非常重要的。

+0

謝謝。我剛來這地方。你能否給我留下更多有關保留以及我們在這裏做什麼的信息? –

+0

感謝它的工作。 –