Ç& IOS的n00b这里数据的tableview,填充使用JSON和AFNetworking的NSDictionary
我一直在这个问题上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
非常感谢您的帮助。这个网站的规则。
严,谢谢你的帮助,但是这把我基本上是相同的运行时错误我已经得到:' - [__ NSCFString objectForKeyedSubscript:]:无法识别的选择发送到实例0x8cb3b20 2014年6月8日12:34: 53.580 HopShack [491:60b] ***终止应用程序由于未捕获的异常'NSInvalidArgumentException',原因:' - [__ NSCFString objectForKeyedSubscript:]:无法识别的选择器发送到实例0x8cb3b20'任何建议? – Blake
严,另外,单步跳过一个断点说我的应用程序崩溃时,我将对象添加到临时阵列 – Blake
嗨。我更新了答案。 – Yan