iPhone JSON到TableView [英] IPhone JSON to TableView

查看:127
本文介绍了iPhone JSON到TableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码有问题,它返回一个错误,提示...

Im having issues with my code, its returning an error that says...

2011-12-24 22:52:36.280 BusinessManager [479:20b] *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'* -[NSCFDictionary isEqualToString:]:无法识别的选择器发送到实例0x3e965e0'>

2011-12-24 22:52:36.280 BusinessManager[479:20b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSCFDictionary isEqualToString:]: unrecognized selector sent to instance 0x3e965e0'>

这是代码:

 #import "BusinessManagerAppDelegate.h"
 #import "ProspectViewController.h"
 #import "JSON.h"

 @implementation ProspectViewController

 @synthesize jsonArray;

- (void)viewDidLoad {
NSURL *jsonURL = [NSURL URLWithString:@"https://www.mysite.php"];
NSString *jsonData = [[NSString alloc] initWithContentsOfURL:jsonURL];

NSLog(jsonData);
self.jsonArray = [jsonData JSONValue]; 

[jsonURL release];
[jsonData release];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section      {
return [jsonArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    
static NSString *Prospects = @"agencyname";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Prospects];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:Prospects] autorelease];
}

cell.text = (NSString *)[self.jsonArray objectAtIndex:indexPath.row];
return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath   *)indexPath {

}

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
}

- (void)viewDidDisappear:(BOOL)animated {
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {   
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; 
}

- (void)dealloc {
[jsonArray dealloc];
[super dealloc];
}

@end

我很确定我已正确设置了所有内容,并且JSON在控制台中正确返回.

Im pretty sure I have everything set up correctly and the JSON is returning correctly in the console.

推荐答案

cell.textLabel.text = (NSString *)[self.jsonArray objectAtIndex:indexPath.row];
(请注意,原始代码正在访问cell.text而不是cell.textLabel.text)

cell.textLabel.text = (NSString *)[self.jsonArray objectAtIndex:indexPath.row];
( Note that the original code was accessing cell.text rather than cell.textLabel.text)

此行可能是错误.让我们逐步看一下它:
1. JSON输出是一个数组,存储在jsonArray中(检查并确保它也不是字典).
2. [self.jsonArray objectAtIndex:indexPath.row]可能是NSDictionary.从返回的异常中可以看到,它涉及一个NCSFDictionary.实际上,很多时候,JSON输出是字典数组
3.出现错误'NSInvalidArgumentException',原因:'*-[NSCFDictionary isEqualToString:]:无法识别的选择器已发送到实例0x3e965e0'> ,代码正在尝试将NSDictionary与NSString进行比较. > 4.要解决此问题,请更仔细地查看JSON输出并进行剖析!并确保JSON输出随大小写而不同(使用不同的URL).

This line is likely the error. Let's look at it step by step:
1. The JSON output is an array, stored in jsonArray (check to make sure it's not a dictionary too).
2. [self.jsonArray objectAtIndex:indexPath.row] is likely an NSDictionary. As you can see from the exception that's returning, it involves a NCSFDictionary. In fact, many times, JSON outputs are arrays of dictionaries
3. With the error 'NSInvalidArgumentException', reason: '* -[NSCFDictionary isEqualToString:]: unrecognized selector sent to instance 0x3e965e0'>, the code is trying to compare an NSDictionary to an NSString.
4. To solve this, look at the JSON output more carefully and dissect it! And make sure that the JSON output isn't varying from case to case (with different URLs).

这篇关于iPhone JSON到TableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆