所以我正在开发一个解析json并尝试将结果放入普通UITableview的iphone应用程序。我在xib文件中设置了tableview设置,并将数据源和委托设置为MainViewController(这是实用程序应用程序)。我得到JSON请求就好了,我总是通过将它输出到文本标签来测试它。但是当我调试并将鼠标悬停在cellData var(包含此数据)上时,它显示超出范围,并且我的模拟器显示一个不会滚动的空表...就像它等待某些东西完成一样。
以下是我对视图控制器的标题:
#import "FlipsideViewController.h"
@interface MainViewController : UIViewController <FlipsideViewControllerDelegate, UITableViewDelegate, UITableViewDataSource> {
IBOutlet UILabel *label;
UITableView *theTableView;
NSMutableArray *cellData;
}
@property (nonatomic, retain) IBOutlet UILabel *label;
@property (retain) NSMutableArray *cellData;
- (IBAction)showInfo:(id)sender;
@end
这是我的实施:
#import "MainViewController.h"
#import "JSON.h"
@implementation MainViewController
@synthesize label;
@synthesize cellData;
//Begin code for Table View Data Source (required methods)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//how many rows in the section; for testing purposes...placed here
return [cellData count];
//return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//create a cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
//fill it with contents
cell.textLabel.text = [cellData objectAtIndex:indexPath.row];
//return it
return cell;
}
//End code for Table View Data Source
//Begin code for JSON stuffs
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
//TODO: need to extract this code out to a function because need to make multiple requests per run of the app....
//responseData = [[NSMutableData data] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://tabular.heroku.com/tab_stores/tester.json"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
cellData = [[NSMutableArray alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSMutableData *mutData = [[NSMutableData alloc] init];
[mutData appendData:data];
NSString *responseString = [[NSString alloc] initWithData:mutData encoding:NSUTF8StringEncoding];
NSArray *responseData = [responseString JSONValue];
NSMutableString *text = [NSMutableString stringWithString:@"T:"];
for (int i = 0; i < [responseData count]; i++)
[text appendFormat:@"%@\n", [responseData objectAtIndex:i]];
label.text = text;
NSLog(@"%@\n", cellData);
//Let's parse the data the way its supposed to be
//for every window, there should be a section.
for (int i=0; i<[responseData count]; i++) {
//manage table section
//maybe print window 1
}
//[cellData addObject:[responseData objectAtIndex:0]];
[cellData addObjectsFromArray:responseData];
// [responseData release];
NSLog(@"%@\n", cellData);
[theTableView reloadData];
[responseString release];
[mutData release];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
label.text = [NSString stringWithFormat:@"Connection failed: %@", [error description]];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];
//NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
//[responseData release];
//cellData = [responseString JSONValue];
//NSLog(@"this is in tabs %@", tabs);
// NSLog(@"this is in cellData %@", cellData);
// NSLog(@"this is in cellDatas %@", cellDatas);
//NSMutableString *text = [NSMutableString stringWithString:@"T:"];
// for (int i = 0; i < [cellDatas count]; i++)
// [text appendFormat:@"%@\n", [cellDatas objectAtIndex:i]];
// label.text = text;
//[theTableView reloadData];
}
//End code for JSON stuffs
.
.
.
- (void)dealloc
{
[theTableView release];
//[cellData release];
[super dealloc];
}
我知道tableview首先调用了sectionInInTableView和numberOfRowsInSection的那些函数,所以它不会获取数据,但后来我认为调用reloadData会起作用......但没有。任何人都可以帮助新手吗?
* NSLogs在控制台中:
2010-08-15 15:42:07.421 Tabular[33046:207] (
)
2010-08-15 15:42:09.902 Tabular[33046:207] (
(
(
Google,
"https://www.google.com/"
),
(
"https://tabular.heroku.com/tab_stores/tester",
"https://tabular.heroku.com/tab_stores/tester"
),
(
"test - Google Search",
"http://www.google.com/search?client=safari&rls=en&q=test&ie=UTF-8&oe=UTF-8"
),
(
"Using POST method in XMLHTTPRequest(Ajax)",
"http://www.openjs.com/articles/ajax_xmlhttp_using_post.php"
),
(
"json stringify - Google Search",
"http://www.google.com/search?client=safari&rls=en&q=json stringify&ie=UTF-8&oe=UTF-8"
),
(
"json.stringify escape - Google Search",
"http://www.google.com/search?q=json.stringify escape&hl=en&safe=off&client=safari&rls=en&ei=PUlaTNiYL4O78gbt8bXsAg&start=10&sa=N"
)
),
(
(
Google,
"https://www.google.com/"
)
)
)
答案 0 :(得分:1)
cellData
中的每个项目本身就是另一个数组。它不是一个NSString,所以将它分配给文本字段的“文本”是不可能的(我很惊讶它没有崩溃)。如有疑问,请调用[obj description]
,它应返回NSString。connection:didReceiveData:
。您注释掉的代码在正确的轨道上(缓冲所有数据一次并处理connectionDidFinishLoading:
中的所有内容;或者,如果您有一个基于流的API处理它;我不认为JSON库是您的使用支持)。