2014-06-05 74 views
1

Ç& 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 

非常感谢您的帮助。这个网站的规则。

回答

0

如果你是self.beers数组被正确填充,那么我认为你不重载tableview。

添加此行之后self.beers=[[NSArray alloc]initWithArray:tempArray];

dispatch_async(dispatch_get_main_queue(), ^{ 
       [self.tableView reloadData] 
      }); 

您收到的所有数据后,将刷新实现代码如下。 如果这不起作用,然后NSLog(@"%@",self.beers);,以确保自我。啤酒是人口稠密的。

我没有意识到在实际填充tableView之前它会给你一个错误。我认为问题在于name返回NSString而不是NSArray。实际上,responseObject包含字典数组,因此当您遍历数组时,您会得到每个啤酒的字典。这种方式可以只是objectForKey

Beer *newBeer=[[Beer alloc]initWithName:[oneDictionary [email protected]"name"]]; 

访问啤酒名称检索啤酒的名称。

让我知道它是否成功。

+0

严,谢谢你的帮助,但是这把我基本上是相同的运行时错误我已经得到:' - [__ NSCFString objectForKeyedSubscript:]:无法识别的选择发送到实例0x8cb3b20 2014年6月8日12:34: 53.580 HopShack [491:60b] ***终止应用程序由于未捕获的异常'NSInvalidArgumentException',原因:' - [__ NSCFString objectForKeyedSubscript:]:无法识别的选择器发送到实例0x8cb3b20'任何建议? – Blake

+0

严,另外,单步跳过一个断点说我的应用程序崩溃时,我将对象添加到临时阵列 – Blake

+0

嗨。我更新了答案。 – Yan