我很难用 json 提要日期字段填充 tableview 单元格。我认为这与我获取日期的方式有关
NSMutableArray *datesArray = [[NSMutableArray alloc] init];
for (NSDictionary *tempDict in json){
[datesArray addObject:[tempDict objectForKey:@"date"]];
}
如果可以,请提供帮助。我已经经历了我能想到的一切(仍在学习)。
.h 文件
#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 文件
- (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];
}
这是我的 datesArray 的 NSLog
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"
这是 viewDidLoad 的样子
- (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;
}