I have an array _locations which is populated from a server through a json query. I now want this array to populate a table that I have created, however it does not and it quits on [self.delegate itemsDownLoaded:_locations];.
If I look in the log I can see that _locations does get populated however, but it does not parse on that data.
Here is the full code for HomeModel2:
#import "HomeModel2.h"
#import "Location2.h"
#interface HomeModel2(){
NSMutableData *_downloadedData;
}
#end
#implementation HomeModel2
- (void) downLoadItems{
// Download the json file
NSURL *jsonFileUrl = [NSURL URLWithString:#"http://server.com/service_2.php"];
// Create the request
NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:jsonFileUrl];
// Create the NSURLConnection
[NSURLConnection connectionWithRequest:urlRequest delegate:self];
}
#pragma mark NSURLConnectionDataProtocol Methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// Initialize the data object
_downloadedData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the newly downloaded data
[_downloadedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// Create an array to store the locations
NSMutableArray *_locations = [[NSMutableArray alloc] init];
// Parse the JSON that came in
NSError *error;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:_downloadedData options:NSJSONReadingAllowFragments error:&error];
// Loop through Json objects, create question objects and add them to our questions array
for (int i = 0; i < jsonArray.count; i++)
{
NSDictionary *jsonElement = jsonArray[i];
// Create a new location object and set its props to JsonElement properties
Location2 *newLocation = [[Location2 alloc] init];
newLocation.name = jsonElement[#"Name"];
newLocation.address = jsonElement[#"Address"];
newLocation.latitude = jsonElement[#"Latitude"];
newLocation.longitude = jsonElement[#"Longitude"];
// Add this question to the locations array
[_locations addObject:newLocation];
}
// Ready to notify delegate that data is ready and pass back items
if (self.delegate)
{
[self.delegate itemsDownLoaded:_locations];
}
}
#end
.. and this is the ViewController to publish the data:
#import "ViewController.h"
#import "Location2.h"
#interface ViewController (){
HomeModel2 *_homeModel;
NSArray *_feedItems;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Set this view controller object as the delegate and data source for the table view
self.listTableView.delegate = self;
self.listTableView.dataSource = self;
// Create array object and assign it to _feedItems variable
_feedItems = [[NSArray alloc] init];
// Create new HomeModel2 object and assign it to _homeModel variable
_homeModel = [[HomeModel2 alloc] init];
// Set this view controller object as the delegate for the home model object
_homeModel.delegate = self;
// Call the download items method of the home model object
[_homeModel downLoadItems];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)itemsDownLoaded:(NSArray *)items
{
// This delegate method will get called when the items are finished downloading
// Set the downloaded items to the array
_feedItems = items;
// Reload the table view
[self.listTableView reloadData];
}
#pragma mark Table View Delegate Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of feed items (initially 0)
return _feedItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Retrieve cell
NSString *cellIdentifier = #"BasicCell";
UITableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
// Get the location to be shown
Location2 *item = _feedItems[indexPath.row];
// Get references to labels of cell
myCell.textLabel.text = item.address;
return myCell;
}
#end
Can you confirm that there is "location" objects after this piece of code:
// Set the downloaded items to the array
_feedItems = items;
or is the delegate method not even called?
i don't understand your wording...
I found the problem. I thick it all has to do with the delegate.self method. I read that using this in several places in your code is not the best thing and that the references easily can get messed up if you update and compile frequently. So i flushed all my old compiled data, and ran the app again from scratch and it worked!
I don't believe you ever define the delegate property, or a protocol for the delegate.
As such, you have no way of ensuring the view controller responds to the methods defined in the delegates protocol.
Can you see if the delegate method in the view controller ever gets called?
Instead of below code
if (self.delegate)
{
[self.delegate itemsDownLoaded:_locations];
}
Use
// Set the downloaded items to the array
_feedItems = [NSArray arrayWithArray:items];
// Reload the table view
[self.listTableView reloadData];
Or Change this line of code and check
Instead of
_feedItems = items;
Use
_feedItems = [NSArray arrayWithArray:items];
Hope it helps you...!
Related
I used a data object to store an array data and when the data load completes, I have a block callback. But the problem is that there are different instances in the two methods:
#implementation DWHomeData
- (instancetype)initWithDataLoadCompletion:(void (^)(BOOL))completion
DWHomeData *data = [DWHomeData new];
data.dwStatus = [#[] mutableCopy];
_completion = [completion copy];
[self loadStatusData];
return data;//<DWHomeData: 0x7fb481546860>
}
- (void)loadStatusData {
DWHomeParam *param = [DWHomeParam new];
[DWHomeTool fetchHomeStatusWithParam:param success:^(NSArray *statusArr) {
self.dwStatus = statusArr;//self address:<DWHomeData: 0x7fb481548b00>
_completion(YES);
} failure:^(NSError *error) {
}];
}
#end
My callback is:
- (void)viewDidLoad {
[super viewDidLoad];
_homeData = [[DWHomeData alloc] initWithDataLoadCompletion:^(BOOL success) {
[self.tableView reloadData];
}];//_homeData address:<DWHomeData: 0x7fb481546860>
}
It's because you are allocing it twice. The method new is just a wrapper for an alloc and an init.
So when you call [[DWHomeData alloc] initWith... you allocated memory for the first instance of DWHomeData.
Then, inside the initWith... method you are calling new which allocated memory for the second instance of DWHomeData and you return that second instance, but you call loadStatusData on the first instance.
The easiest solution would be to replace that new call with the standard:
self = [super init]; // no alloc
if (self) {
// initialize properties and call methods
}
return self;
Or you can do how I like to do all the time:
+ (instancetype)dataWithCompletion:(void (^)(BOOL))completion { // static method
DWHomeData *data = [DWHomeData new]; // alloc needed
if (data) {
[data loadStatusData];
}
return data;
}
and then call it without allocating:
_homeData = [DWHomeData dataWithCompletion:^(BOOL success) {
[self.tableView reloadData];
}];
so the alloc is wrapped inside the static init method and there is no need to call it outside.
I have a custom NSCoding class which stores and retrieves itself when necessary. However, it doesn't feed data to my table view until giving one entry to the array of custom Person objects inside it and restarting the app, then giving another. The first one disappears, however.
After that, it appears to load okay.
Here is the implementation of the class
#import "DataStorage.h"
#implementation DataStorage
#synthesize arrayOfPeople = _arrayOfPeople;
+ (DataStorage *)sharedInstance
{
static DataStorage *state = nil;
if ( !state )
{
NSData *data =[[NSUserDefaults standardUserDefaults] objectForKey:#"DataStorageBank"];
if (data)
{
state = [NSKeyedUnarchiver unarchiveObjectWithData:data];
}
else
{
state = [[DataStorage alloc] init];
}
}
return state;
}
- (instancetype)initWithCoder:(NSCoder *)decoder
{
self = [self init];
if (self) {
if ([decoder decodeObjectForKey:#"DataStoragePeopleArray"]) {
_arrayOfPeople = [[decoder decodeObjectForKey:#"DataStoragePeopleArray"] mutableCopy];
} else {
_arrayOfPeople = [[NSMutableArray alloc] init];
}
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:_arrayOfPeople forKey:#"DataStoragePeopleArray"];
}
- (void)save
{
NSData *appStateData = [NSKeyedArchiver archivedDataWithRootObject:self];
[[NSUserDefaults standardUserDefaults] setObject:appStateData forKey:#"DataStorageBank"];
}
#end
I add objects to the _arrayOfPeople like so:
Person *person = [[Person alloc] initWithFirstName:firstName personSurname:surname personCompay:company personPosition:position personEmail:email personMobile:mobile personProduct:product];
[[DataStorage sharedInstance].arrayOfPeople addObject:person];
[[DataStorage sharedInstance] save];
And load them into the table view by this:
Person *personAtIndex = [[DataStorage sharedInstance].arrayOfPeople objectAtIndex:indexPath.row];
[_arrayOfPeople addObject:personAtIndex];
cell.textLabel.text = personAtIndex.firstName;
cell.detailTextLabel.text = personAtIndex.surname;
Loading them in to the table view is in the method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
It looks like you only initialize _arrayOfPeople in initWithCoder:. However, if your data does not exist in user defaults already, you use state = [[DataStorage alloc] init] to initialize your shared instance. This does not call initWithCoder: so _arrayOfPeople is nil until after you save and load again, when it is finally initialized as [[NSMutableArray alloc] init]. To fix this, move _arrayOfPeople = [[NSMutableArray alloc] init] out of initWithCoder: and into init. (You could alternatively move it into sharedInstance, but it makes more sense in init since it is not specific to configuring the shared instance.)
Unrelated, but also make sure you synchronize.
- (void)save
{
NSData *appStateData = [NSKeyedArchiver archivedDataWithRootObject:self];
[[NSUserDefaults standardUserDefaults] setObject:appStateData forKey:#"DataStorageBank"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
I am a quite new to IOS development and keep having struggle with it. I would like to display phone list which an user has from my server but tableview does not display items. I have got data from server well and I think settings for UItableView is correct. Here is my code:
STKPhoneHolderViewController.h
#import <UIKit/UIKit.h>
#import "STKSimpleHttpClientDelegate.h"
#interface STKPhoneHolderViewController : UITableViewController <UITableViewDataSource, STKSimpleHttpClientDelegate>
#property (strong, nonatomic) IBOutlet UITableView *phoneTable;
#property (strong, nonatomic) NSMutableArray *phoneArray;
#end
STKPhoneHolderViewController.m
#implementation STKPhoneHolderViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.phoneTable.dataSource = self;
self.phoneArray = [[NSMutableArray alloc]init];
[self loadPhoneList];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.phoneArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"PhoneCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
STKPhoneHolder *phoneHolder = [self.phoneArray objectAtIndex:indexPath.row];
[cell.textLabel setText:phoneHolder.userName];
return cell;
}
#pragma Custom method
- (void) loadPhoneList
{
self.phoneArray = [[NSMutableArray alloc]init];
STKSimpleHttpClient *client = [[STKSimpleHttpClient alloc]init];
client.delegate = self;
NSString *userId = #"your_id_h";
NSString *sUrl = [NSString stringWithFormat:#"%#%#?userid=%#",
MOBILE_API_URL,
PHONEHOLDER_URI,
userId];
[client send:sUrl data:#""];
}
#pragma STKSimpleHttpClientDelegate
-(void) complete:(STKHttpResult*) result
{
if (result.ok != YES){
[STKUtility alert:result.message];
return;
}
self.phoneArray = (NSMutableArray*)result.result;
for (STKPhoneHolder *holder in self.phoneArray) {
NSLog(#"%#", [holder description]);
}
[self.phoneTable reloadData];
NSLog(#" isMainThread(%d)", [NSThread isMainThread] );
}
#end
STKSimpleHttpClient.m
#import "STKSimpleHttpClient.h"
#import "STKSimpleHttpClientDelegate.h"
#implementation STKSimpleHttpClient
NSMutableData *responseData;
STKHttpResult *httpResult;
void (^completeFunction)(STKHttpResult *);
- (void) send:(NSString*)url
data:(NSString*)data
{
httpResult = [[STKHttpResult alloc]init];
dispatch_async(dispatch_get_main_queue(), ^{
if ( data == nil) return;
//Get request object and set properties
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL: [NSURL URLWithString: url]];
//set header for JSON request and response
[urlRequest setValue:#"application/json; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[urlRequest setValue:#"application/json" forHTTPHeaderField:#"Accept"];
//set http method to POST
[urlRequest setHTTPMethod:#"POST"];
//set time out
[urlRequest setTimeoutInterval:20];
NSData *body = [data dataUsingEncoding:NSUTF8StringEncoding];
//set request body
urlRequest.HTTPBody = body;
//connect to server
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
if (conn==nil){
//Do something
}
});
}
#pragma mark - NSURLConnection Delegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
// A response has been received, this is where we initialize the instance var you created
// so that we can append data to it in the didReceiveData method
// Furthermore, this method is called each time there is a redirect so reinitializing it
// also serves to clear it
responseData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Append the new data to the instance variable you declared
[responseData appendData:data];
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse*)cachedResponse {
// Return nil to indicate not necessary to store a cached response for this connection
return nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// The request is complete and data has been received
// You can parse the stuff in your instance variable noow
NSError *error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
BOOL ok = [[json objectForKey:#"ok"] boolValue];
NSString *message = [json objectForKey:#"message"];
if (ok == NO) {
[httpResult setError:message];
} else {
[httpResult setSuccess:[json objectForKey:#"result"]];
}
if (self.delegate !=nil) {
[self.delegate complete:httpResult];
}
responseData = nil;
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// The request has failed for some reason!
// Check the error var
if (self.delegate !=nil) {
[self.delegate complete:[httpResult setError:#"Connection failed."]];
}
}
STKPhoneHolder.m
#import <Foundation/Foundation.h>
#interface STKPhoneHolder : NSObject
#property NSString *deviceId;
#property NSString *userId;
#property NSString *userName;
#property NSString *msn;
- (id) initWithDeviceId:(NSString*)aDeviceId
userId:(NSString*)aUserId
userName:(NSString*)aUserName
msn:(NSString*)aMsn;
#end
Log:
2013-12-17 16:14:23.447 [5323:70b] {
deviceId = 11111;
email = "";
msn = 11111111;
role = "";
userId = aaaaaa;
userName = "Joshua Pak";
}
2013-12-17 16:14:23.448 [5323:70b] {
deviceId = 22222;
email = "";
msn = 2222222;
role = "";
userId = bbbbb;
userName = "Jasdf Pak";
}
2013-12-17 16:14:23.449 Stalker[5323:70b] isMainThread(1)
I can see the log printing phoneArray with two phones in 'complete' method but tableview just display "No record". Tableview does not render again even though I called reloadData method. I made sure that [self.phoneTable reloadData] is called in debugging mode.
What do I have to do more?
Try to call reloadData in main thread
#pragma STKSimpleHttpClientDelegate
-(void) complete:(STKHttpResult*) result
{
if (result.ok != YES){
[STKUtility alert:result.message];
return;
}
self.phoneArray = (NSMutableArray*)result.result;
for (STKPhoneHolder *holder in self.phoneArray) {
NSLog(#"%#", [holder description]);
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.phoneTable reloadData];
}
}
Or you can use performSelectorOnMainThread
[self.phoneTable performSelectorOnMainThread:#selector(reloadData)
withObject:nil
waitUntilDone:NO];
I am guessing STKSimpleHttpClient class is calling complete delegate function on different thread, all user interface interaction suppose to be called from main thread.
Try this code to see which thread you are in from the complete delegate function
NSLog(#" isMainThread(%d)", [NSThread isMainThread] );
check this. does the code load the tableview before you get information from web services. if so then write the statement [tableview Reload]; next to the web services information getting process. This will help
It's not necessary to specify the number of sections, but you might want to do it with this code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
I see you're using a Table View Controller, which already has a tableView reference self.tableView.
Like #rdelmar said, you could use that reference instead of your phoneTable:
[[self tableView] setDataSource:self];
[[self tableView] setDelegate:self];
[[self tableView] reloadData];
Short background: I am creating an app in which the user can see the menus for each restaurant. I have created a class called "Dish" in which the name, ingredients and price of a menu item is set. I have then created a class for each restaurant which creates and stores the items for each restaurant. I want the UITableView, in which I display the menus, to divide the parts of the menus (for example "main dishes", desserts") into the table sections.
Ideally, I would create an array with the parts for each restaurant menu, in which a number of array (one for every menu part) containing the dishes in that part. I have tried this, and cannot get it to work. It seems like I cannot pass an array containing an array, since it will only return nil. Is this really the case?
Under is the code for my class containing the menu of the restaurant called Badholmen, which now only contains twi dishes. But other menus I have contain up to 70 dishes and there I need to be able to put every dish in an array, and the [allBadholmen addObject:ArrayOfMenuParts:
#import "BadholmenStore.h"
#import "Dish.h"
#implementation BadholmenStore
+ (BadholmenStore *)sharedStore
{
static BadholmenStore *sharedStore = nil;
if (!sharedStore)
sharedStore = [[super allocWithZone:nil] init];
return sharedStore;
}
#pragma mark - tillägg av platser
- (void)createBadholmen
{
Dish *bhvar = [[Dish alloc] initWithName:#"Varierande meny"
ingredients:#" "
price:#" "];
[allBadholmen addObject:bhvar];
Dish *bhsom = [[Dish alloc] initWithName:#"Sommarlunch med varierande meny (endast sommartid)"
ingredients:#"inkl. måltidsdryck"
price:#"79:-"];
[allBadholmen addObject:bhsom];
}
- (void)emptyArray
{
[allBadholmen removeAllObjects];
}
- (NSArray *)allBadholmen
{
return allBadholmen;
}
#pragma mark - Overriden methods
+ (id)allocWithZone:(NSZone *)zone
{
return [self sharedStore];
}
- (id)init
{
self = [super init];
if (self) {
allBadholmen = [[NSMutableArray alloc] init];
}
return self;
}
#end
To make the table view aware of the dishes in the menu, I call (in the init method of the table view):
[[BadholmenStore sharedStore] emptyArray];
[[BadholmenStore sharedStore] createBadholmen];
Do like this, hope this helps u :)
// in BadholmenStore.h
#import <Foundation/Foundation.h>
#interface BadholmenStore : NSObject
{
NSMutableArray *allBadholmen; //for your shared object contains one "mutable array", define it hear
}
+ (BadholmenStore *)sharedStore; //your class method
//instance methods
- (void)createBadholmen;
- (void)emptyArray;
- (NSMutableArray *)allBadholmen;
#end
// in BadholmenStore.m
#import "BadholmenStore.h"
#import "Dish.h"
#implementation BadholmenStore
static BadholmenStore *sharedStore = nil; //it shoud be visible to all, put this line hear
+ (BadholmenStore *)sharedStore
{
if (!sharedStore)
sharedStore = [[super allocWithZone:nil] init];
return sharedStore;
}
#pragma mark - tillägg av platser
- (void)createBadholmen
{
Dish *bhvar = [[Dish alloc] initWithName:#"Varierande meny"
ingredients:#" "
price:#" "];
[allBadholmen addObject:bhvar];
Dish *bhsom = [[Dish alloc] initWithName:#"Sommarlunch med varierande meny (endast sommartid)" ingredients:#"inkl. måltidsdryck" price:#"79:-"];
[allBadholmen addObject:bhsom];
}
- (void)emptyArray
{
[allBadholmen removeAllObjects];
}
- (NSMutableArray *)allBadholmen // replace your NSArray with NSMutableArray
{
return allBadholmen;
}
#pragma mark - Overriden methods
+ (id)allocWithZone:(NSZone *)zone
{
return [self sharedStore];
}
- (id)init
{
self = [super init];
if (self) {
allBadholmen = [[NSMutableArray alloc] init];
}
return self;
}
#end
//in the class where u are using this shared class object
[super viewDidLoad];
[[BadholmenStore sharedStore] emptyArray];
[[BadholmenStore sharedStore] createBadholmen];
NSMutableArray *allObjects = [[BadholmenStore sharedStore] allBadholmen]; // now use allObjects
I can't make my table view show my data, the array has valid data by the NSLog output. I put a breakpoint at the beginning of tableView:cellForRowAtIndexPath: and it never get there. Any ideas why?
#import "ViewController.h"
#import "Ride.h"
#interface ViewController ()
#property (nonatomic, strong) NSMutableData *responseData;
#end
#implementation ViewController
#synthesize rideIds = _rideIds;
#synthesize rideNames = _rideNames;
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"viewdidload");
self.responseData = [NSMutableData data];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
// http://www.strava.com/api/v1/segments/229781/efforts?best=true
// Efforts on segment by athlete limited by startDate and endDate
//NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"https://www.strava.com/api/v1/segments/229781/efforts?athleteId=11673&startDate=2012-02-01&endDate=2012-02-28"]];
//Leader Board on Segment all Athletes
//NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"https://www.strava.com/api/v1/segments/229781/efforts?best=true"]];
//Rides by Athlete
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"https://www.strava.com/api/v1/rides?athleteId=10273"]];
//Twitter Example
//NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"https://api.twitter.com/1/trends"]];
//Efforts by Ride
//NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"https://www.strava.com/api/v1/rides/77563/efforts"]];
//Effort Detail
//NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"https://www.strava.com/api/v1/efforts/688432"]];
//Google API Call
//NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false&key=AIzaSyAbgGH36jnyow0MbJNP4g6INkMXqgKFfHk"]];
/* dispatch_async(dispatch_get_main_queue(),^ {
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
} ); */
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(theConnection){
self.rideIds = [[NSMutableArray alloc]init];
self.rideNames = [[NSMutableArray alloc] init];
} else {
NSLog(#"No Connection");
}
}
//Delegate methods for the NSURLConnection
//In order to download the contents of a URL, an application needs to provide a delegate object that, at a minimum, implements the following delegate methods: connection:didReceiveResponse:, connection:didReceiveData:, connection:didFailWithError: and connectionDidFinishLoading:.
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(#"didReceiveResponse");
//This message can be sent due to server redirects, or in rare cases multi-part MIME documents.
//Each time the delegate receives the connection:didReceiveResponse: message, it should reset any progress indication and discard all previously received data.
[self.responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(#"didFailWithError");
NSString *errorDescription = [error description];
// NSLog([NSString stringWithFormat:#"Connection failed: %#", errorDescription]);
NSLog(#"Connection failed: %#", errorDescription);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(#"connectionDidFinishLoading");
NSLog(#"Succeeded! Received %d bytes of data",[self.responseData length]);
// convert to JSON
NSError *myError = nil;
//NSDictionary *jsonRes = [NSJSONSerialization JSONObjectWithData:self.responseData options:NSJSONReadingMutableLeaves error:&myError];
NSDictionary *jsonResult = [NSJSONSerialization JSONObjectWithData:self.responseData options:NSJSONReadingMutableLeaves error:&myError];
NSDictionary *jsonRides =[jsonResult objectForKey:#"rides"];
// Show all values coming out of "rides" key
// Store ride id's and names on arrays for later display on tableview
for (NSDictionary *rides in jsonRides) {
[self.rideIds addObject:[rides objectForKey:#"id"]];
NSLog(#"id = %#", [rides objectForKey:#"id"]);
//NSLog(#"%#",self.rideIds);
[self.rideNames addObject:[rides objectForKey:#"name"]];
NSLog(#"name = %#", [rides objectForKey:#"name"]);
//NSLog(#"%#",self.rideNames);
}
NSLog(#"%#",self.rideIds);
NSLog(#"%#",self.rideNames);
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
// Show all values coming out of NSKSONSerialization
for(id key in jsonResult) {
id value = [jsonResult objectForKey:key];
NSString *keyAsString = (NSString *)key;
NSString *valueAsString = (NSString *)value;
NSLog(#"key: %#", keyAsString);
NSLog(#"value: %#", valueAsString);
}
// extract specific value...
// NSArray *results = [res objectForKey:#"results"];
/*NSArray *results = [res objectForKey:#"rides"];
for (NSDictionary *result in results) {
NSData *athleteData = [result objectForKey:#"name"];
NSLog(#"Ride name: %#", athleteData);
}*/
/* dispatch_async(dispatch_get_main_queue(),^ {
[self.rideTableView reloadData];
} ); */
[self.rideTableView reloadData];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#"tableView:numberOfRowsInSection: ");
//return self.rideIds.count;
NSLog(#"%u",self.rideNames.count);
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"tableView:cellForRowAtIndexPath: ");
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if( nil == cell ) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text= [self.rideNames objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tv
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tv deselectRowAtIndexPath:indexPath animated:YES];
}
#end
NSLog content:
2012-08-18 18:47:29.497 WebServiceCall[10387:c07] viewdidload
2012-08-18 18:47:29.503 WebServiceCall[10387:c07] tableView:numberOfRowsInSection:
2012-08-18 18:47:29.503 WebServiceCall[10387:c07] 0
2012-08-18 18:47:29.504 WebServiceCall[10387:c07] tableView:cellForRowAtIndexPath:
2012-08-18 18:47:29.506 WebServiceCall[10387:c07] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
*** First throw call stack:
(0x14b6022 0xeb6cd6 0x14a2d88 0x395d 0xb3c54 0xb43ce 0x9fcbd 0xae6f1 0x57d42 0x14b7e42 0x1d87679 0x1d91579 0x1d164f7 0x1d183f6 0x1da5160 0x17e84 0x18767 0x27183 0x27c38 0x1b634 0x13a0ef5 0x148a195 0x13eeff2 0x13ed8da 0x13ecd84 0x13ecc9b 0x17c65 0x19626 0x22fd 0x2265 0x1)
terminate called throwing an exception(lldb)
UPDATE: It seems that after passing through viewDidLoad it jumps right into tableview:numberOfRowsInSection method skipping all the 4 methods for handling NSURLConnection (where I updated my arrays).
My view controller is both delegate of my NSURLConnection AND my tableView. It seems that it's running first the tableView methods. Any suggestions as to how to make it run the NSURLConnection methods first ?
Two things you could try -- First, log self.rideIds.count in your numberOfRowsInSection method to make sure it's not returning 0. Second, at the end of your connectionDidFinishLoading method, put in a [tableView reloadData] (or whatever the outlet to your table view is), that should take care of the problem of the table view methods being called before your connection is done.
After Edit: The error "-[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array" is being caused by the "return 3" in your numberOfRowsInSection method. When the app starts up the table view will try to populate itself before your connection returns any results, so numberOfRowsInSection should return 0 not 3 the first time through, which it will do if you put back the return self.rideIds.count line. If you do the reloadData at the end of the connection delegate methods, then the array will be populated and the table view should work properly.
Where is tableView:numberOfSectionsInTableView:? Perhaps that is returning 0 although the default is 1 if not set; You also need to set delegate and dataSource on your tableView.