UITableview在numberOfRowsInSection崩溃;NSDictionary中的Json到UITableview

UITableview在numberOfRowsInSection崩溃;NSDictionary中的Json到UITableview,json,uitableview,nsdictionary,crash,Json,Uitableview,Nsdictionary,Crash,我已经在initWithNibName中将Json和JSONKit从url获取到NSDictionary NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:jsonUrl]]; JSONDecoder *jsonKitDecoder = [JSONDecoder decoder]; jsonDic = [jsonKitDecoder parseJSONData:jsonData];

我已经在initWithNibName中将Json和JSONKit从url获取到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数据源和委托给FileOwner

手动将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上显示课程。

如果向数组对象发送消息(此处为计数),则该数组对象可能已解除分配,导致崩溃(EXEC\u BAD\u ACCESS?)。排队

array = [jsonDic objectForKey:@"courses"];    // NSArray *array
看起来它是自动释放的,你应该保留它

编辑:

您可以通过以下方式保留它:

将其设置为保留属性

@property(nonatomic, retain) NSArray* array;
以及使用访问器

self.array = [jsonDic objectForKey:@"courses"]; 
它会自动释放旧值并保留新值。或者明确地保留它

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

然后,您一定不要忘记在完成后释放它,否则您将泄漏内存。您应该阅读,它为您提供了一个全面的解释,了解基本技术非常重要。

这就是NSLog中的内容:…经过一些。。。gdb-i386-apple-darwin(26422,0x778720)malloc:**mmap(大小=1420296192)失败(错误代码=12)***错误:无法分配区域***在malloc\u error\u break中设置一个断点以进行调试…经过一些。。。gdb堆栈在内部错误点爬网:…经过一些/SourceCache/gdb/gdb-967/src/gdb/utils.c:1144:内部错误:虚拟内存耗尽:无法分配1420296192字节。检测到GDB内部存在问题,进一步调试可能不可靠。调试器已退出,状态为1。调试器已退出,状态为1。如果您取消删除有关数组洗牌的问题,我已写下答案。谢谢。我是新来的。你能告诉我更多关于保留的信息吗?我们在这里是怎么做的?