所以我正在研究解析json並嘗試將結果放入純UITableview的iphone應用程序。我在xib文件中有我的tableview設置,並且還將數據源和委託設置爲MainViewController(這是實用程序應用程序)。我得到的JSON請求很好,我總是通過輸出到文本標籤來測試。但是,當我調試並將鼠標懸停在cellData var(包含此數據)上時,它顯示超出範圍,並且我的模擬器顯示一個不會滾動的空表...就像它等待某些事情完成一樣。NSMutable Array中的JSON數據超出範圍,tableView將不會顯示cellData
這裏是我的視圖控制器頭:
#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的sectionsiInTableView和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/"
)
)
)
你可以顯示你的NSLog的控制檯輸出。有代表性的部分會這樣做。 – tonklon 2010-08-15 19:21:24