【发布时间】:2014-06-05 21:16:36
【问题描述】:
这里是c & ios n00b,
我已经解决这个问题 3 天了。我担心我缺少一个基本概念。我已经研究并完成了与此相关的每个教程和堆栈溢出问题,但我无法得到答案。我正在尝试使用 json 文件中的数据填充我的 tableviewcontroller。我可以连接并从 JSON 文件输出,但我似乎无法解析字典并实际使用字典。这是我认为最接近正确的代码:
TABLEVIEW CONTROLLER
#import "AllTableViewController.h"
#import "AFHTTPRequestOperation.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"
#import "Beer.h"
@interface AllTableViewController ()
@end
@implementation AllTableViewController
- (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;
NSURL *URL = [NSURL URLWithString:@"http://www.hopshack.com/db_get_all_beer.php"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@",responseObject[@"beer"][3][@"name"]);
NSMutableArray *tempArray=[[NSMutableArray alloc]init];
for(NSDictionary *oneDictionary in responseObject){
Beer *newBeer=[[Beer alloc]initWithName:oneDictionary[@"beer"][@"name"]];
[tempArray addObject:newBeer];
}
self.beers=[[NSArray alloc]initWithArray:tempArray];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Beer"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];
}];
[operation start];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.beers.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pCell" forIndexPath:indexPath];
if(cell==nil){
cell=[[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"pCell"];
}
cell.textLabel.text=[self.beers[indexPath.row] name];
return cell;
}
BEER MODEL
#import "Beer.h"
#import "AFHTTPRequestOperation.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"
@implementation Beer
-(id)initWithName:(NSString *)aName{
self=[super init];
if(self){
self.name=aName;
}
return self;
}
@end
在此先感谢大家的帮助。本站规则。
【问题讨论】:
标签: ios objective-c json uitableview