i hope somebody can help me. i want to fetch a json file and update a tableview with the data. that's my .h
PHP Code:
@interface FirstViewController : UITableViewController {
UITableView *tableView;
NSDictionary *listData;
NSMutableData *responseData;
}
@property (nonatomic, retain) NSDictionary *listData;
@property (nonatomic, retain) NSMutableData *responseData;
@property (nonatomic, retain) IBOutlet UITableView *tableView;
and my .m
PHP Code:
@synthesize listData;
@synthesize responseData;
@synthesize tableView;
- (void)viewDidLoad
{
self.listData = nil;
// load external data
responseData = [[NSMutableData data] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://JSON_URL"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
[super viewDidLoad];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Connection failed: %@", [error description]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];
NSError *error = nil;
NSDictionary *dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:responseData error:&error];
self.listData = dictionary;
[self.tableView reloadData]; // <--------- breaks it, but why??
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.listData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CustomCell";
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (CustomCell *) currentObject;
break;
}
}
}
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)dealloc {
[responseData release];
[listData release];
[super dealloc];
}
after fetching the json data (connectionDidFinishLoading) the [self.tableView reloadData] breaks the app. but why? i've read in so many posts that that is the way the reload a tableview (?)
thanks for any help