-1

I am trying to populate a UITableView with JSON, but when I reload the data of my table after making the connection it causes my app to crash, with error code

"Thread 1: EXC_BAD-ACCESS (code=1, address=0x0040da5)"

Here is my code:

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    data = [[NSMutableData alloc] init];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData {
[data appendData:theData];
}


-(void)connectionDidFinishLoading:(NSURLConnection *)connection {

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

news = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
[mainTableView reloadData];


}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

   UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Could not gather data, please make sure you're connected to either 3G or Wi-F" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
[errorView show];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

}
  • log `news` in connectionDidFinishLoading. – Toseef Khilji Dec 11 '13 at 05:13
  • Be sure you call `reloadData` on the main thread. – rmaddy Dec 11 '13 at 05:14
  • could you share the value 'news' have!! means log and show here. Also try using 'news = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];' – Ravi Sisodia Dec 11 '13 at 05:15
  • Are you sure about the data(news) you're getting in connectionDidFinishLoading is correct ? – aToz Dec 11 '13 at 05:16
  • NSlog news and add table methods here – Pradhyuman sinh Dec 11 '13 at 05:17
  • Just try to create a ReferenceArray (news) same as the Json you're expecting from the server in ViewDidLoad and try to populate that, If(itWorks) then there is something wrong with the data(news) in connectionDidFinishLoading method Else something is wrong with the tableView methods. "how are you creating and updating the table view cells ?" – aToz Dec 11 '13 at 05:27
  • Please provide sufficient Data to solve your problem, like tableview methods and NSlog of news and etc. – mAc Dec 11 '13 at 05:53

1 Answers1

0

1> Are you sure you are getting news as expected by your table view ?

2> are you setting news to your datasource of the table before calling reload data

3> Please make sure you are extracting correct values from dictionary in your cellForIndexPath Method

The primary observation is that there is something wrong in cellForRowAtIndexPath and accessing the news array . Please check

amrut1
  • 124
  • 10