I am having a hard time populating the tableview cells with json feed dates field. I think it has do to with the way I am getting the dates
NSMutableArray *datesArray = [[NSMutableArray alloc] init];
for (NSDictionary *tempDict in json){
[datesArray addObject:[tempDict objectForKey:@"date"]];
}
Please assist if you can. I have gone through everything I can think of (still learning).
.h file
#import <UIKit/UIKit.h>
@interface AvailabilityViewController : UIViewController <UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource>
{
NSDate *appointmentdate;
UIActionSheet *dateSheet;
UITextField *mydatetextfield;
UILabel *pastDateLabel;
NSArray *json;
}
//-(IBAction)getDataFromJson:(id)sender;
@property (strong, nonatomic) IBOutlet UITextField *mydatetextfield;
@property (nonatomic, retain) NSDate *appointmentdate;
@property (strong, nonatomic) IBOutlet UILabel *pastDateLabel;
@property (strong, nonatomic) IBOutlet UITableView *_tableView;
@property (nonatomic, retain) NSArray *json;
//-(void)setDate;
-(void)dismissDateSet;
-(void)cancelDateSet;
@end
.m file
- (void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
//NSLog(@"string is %@", responseData);
self.json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSLog(@"string is %@", responseData);
if ([json isKindOfClass:[NSArray class]]) {
NSLog(@"its an array");
} else if ([json isKindOfClass:[NSDictionary class]]) {
NSLog(@"its a dictionary");
} else if ([json isKindOfClass:[NSString class]]) {
NSLog(@"its a string");
} else if ([json isKindOfClass:[NSNumber class]]) {
NSLog(@"its a number");
} else if ([json isKindOfClass:[NSNull class]]) {
NSLog(@"its a null");
} else if (json == nil){
NSLog(@"nil");
}
//NSArray* latestLoans = [json objectForKey:@"date"]; //2
NSMutableArray *datesArray = [[NSMutableArray alloc] init];
for (NSDictionary *tempDict in json){
[datesArray addObject:[tempDict objectForKey:@"date"]];
}
NSLog(@"this is your datesArray %@", datesArray);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return self.json.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [[self.json objectAtIndex:indexPath.row] objectForKey:@"date"];
return cell;
//[_tableView reloadData];
}
Here is my NSLog of datesArray
2012-08-21 10:09:39.303 GBSB[1409:15b03] this is your datesArray (
"2012-08-13 12:00:00",
"2012-08-13 10:00:00",
"2012-08-13 13:00:00"
Here is what the viewDidLoad looks like
- (void)viewDidLoad
{
[super viewDidLoad];
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL:
kLatestKivaLoansURL];
[self performSelectorOnMainThread:@selector(fetchedData:)
withObject:data waitUntilDone:YES];
});
}
- (void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
//NSLog(@"string is %@", responseData);
self.json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSLog(@"string is %@", responseData);
if ([json isKindOfClass:[NSArray class]]) {
NSLog(@"its an array");
} else if ([json isKindOfClass:[NSDictionary class]]) {
NSLog(@"its a dictionary");
} else if ([json isKindOfClass:[NSString class]]) {
NSLog(@"its a string");
} else if ([json isKindOfClass:[NSNumber class]]) {
NSLog(@"its a number");
} else if ([json isKindOfClass:[NSNull class]]) {
NSLog(@"its a null");
} else if (json == nil){
NSLog(@"nil");
}
//NSArray* latestLoans = [json objectForKey:@"date"]; //2
NSMutableArray *datesArray = [[NSMutableArray alloc] init];
for (NSDictionary *tempDict in json){
[datesArray addObject:[tempDict objectForKey:@"date"]];
}
NSLog(@"this is your datesArray %@", datesArray);
NSLog(@"this is the json %@", self.json);
// 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;
}
You are creating a local variable called json in your fetchedData method and putting the parsed response in there. However, because this is a local variable it ceases to exist once your exit from the method.
Instead, what you should do is to put the parsed response data into your viewController’s
json@property which you declare in the .h file. To do this, make this change to yourfetchedData:method:Then in your
cellForRowAtIndexPath:you can pull out the data like this:Also your
numberOfRowsInSection:should return:EDIT:
More explanation about local variables vs. ivars vs. properties…
When you declare this in your view controller’s @interface in the .h file:
you are creating in instance variable (ivar) for your class. Whenever an instance of your class is instantiated it will have a member variable named
jsonthat you can access within the methods of your class.When you declare a property like this:
and a matching @synthesize in the implementation file:
the compiler auto generates a setter and getter method for you, so you can then use these methods:
You can do the same in modern Objective-C using dot notation:
Your property ends up being backed by an ivar, which is by default named the same of the property and will be autogenerated for you if it doesn’t exist. (You can also specify the name of the backing ivar in the @synthesize statement, and you’ll often see people using ivar names that start with an underscore to help keep straight what is the ivar name and what is the property name, but I won’t go into that further here)
Your object’s properties can be accessed from other classes, whereas your object’s ivars cannot.
But back to your question. In addition to your ivar and property, you have created a local variable, also named
jsonin yourfetchedData:method. This variable, because you declare it within the body of the method, will only exist until the method finishes, at which time it will be deallocated and the data contained will be lost if not retained elsewhere. Because you have given your local variable the same name as your ivar, the local variable effective hides the ivar.Apple does not recommend using ivars directly anyway, but instead doing all access through your class properties (getter and setter methods). That’s why I suggested using
self.json. It should also fix your problem, since values saved to your property will persist beyond the execution of the method.Hope that helps some.