English 中文(简体)
OfRowsIn Section; Json in NSDictionary to UITableview
原标题:UITableview crashes at numberOfRowsInSection; Json in NSDictionary to UITableview

我把Json与JSONKit一道,从ur到NSDictionary in in initWith NibName

    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" }  ]
    }

我把一张表格拖到西布,把IBOutlet UITableview tblView拖到关于Interface Buildinger和作为表象数据的表象上,并委托他人存档。

人工添加概览活动

    - (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;
    }

有些人请帮助我。 我需要将课程表上显示。

最佳回答

您的阵列物体可能会被分配到一起造成坠毁(EXEC_BAD_ACCESS?) 如果你向它发出信息(请点)。 从一行

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

似乎像自动释放一样,你应当保留。

EDIT:

您可以保留:

将其列为保留财产

@property(nonatomic, retain) NSArray* array;

接收器的使用

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

自动发放旧价值,保留新价值。 或者明确保留否决权

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

那么,你不能忘记在你做时释放它,否则你就会泄露记忆。 页: 1 《先进记忆管理规划指南》()给您提供了详尽的解释,非常关键的是,你了解基本技术。

问题回答

暂无回答




相关问题
Ruby Interpreter crashes with a certain word

Ok, this one s a little ridiculous, and I m almost afraid no one will believe me. But here it goes: I have written a Ruby Rails application that handles content for tons of domains. Now I know this ...

Java HotSpot error

Curious if anyone could help out in regards to a Java HotSpot dump...saw some reference to head over to the Sun Forums, figured I would try here first...below is the dump... # # An unexpected error ...

NSOperation performSelectorOnMainThread crashes

I m calling a NSOperation from a Subview of a NavigationController. MyOperation *op = [[MyOperation alloc] target:self action:@selector(didFinishOperation)]; The Operation loads some data from a ...

Is this kind of crash report useless?

I tried use "PLCrashReport" to collect the crash info then make the app more stable, but turns out that the report is like this(dont even has a call stack,how am I suppose to use it?): The "Exception:...

Xcode crashes with divide by zero

I downloaded urlcache.zip from http://developer.apple.com/iphone/library/samplecode/URLCache/index.html#//apple_ref/doc/uid/DTS40008061 I opened the project in xcode and clicked on urlcacheconection....

热门标签