【问题标题】:UITableview crashes at numberOfRowsInSection; Json in NSDictionary to UITableviewUITableview 在 numberOfRowsInSection 崩溃; NSDictionary 中的 Json 到 UITableview
【发布时间】: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


【解决方案1】:

如果您向其发送消息(此处为计数),您的数组对象可能已被释放并导致崩溃(EXEC_BAD_ACCESS?)。从线

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

它好像是自动发布的,你应该保留它。

编辑:

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

将其设置为保留属性

@property(nonatomic, retain) NSArray* array;

并使用访问器

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

它会自动释放旧值并保留新值。或者明确地保留它

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

那么你一定不要忘记在完成后释放它,否则你会泄漏内存。您应该阅读Advanced Memory Management Programming Guide,它为您提供了详尽的解释,了解基本技术非常重要。

【讨论】:

  • 谢谢。我刚来这地方。你能告诉我更多关于保留和我们在这里如何做的信息吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 2018-07-15
  • 1970-01-01
  • 1970-01-01
  • 2011-09-27
相关资源
最近更新 更多