Web api and tableView problems



我是Objective C和Xcode的新手,但我每天都在学习一点!:)我试图建立一个应用程序,将显示一个web api在我的表视图,但它不显示…当我NSLog它时,它显示我的搜索工作并且它得到了我正在寻找的数据但不幸的是它不会显示在我的tableView .

如果有人有时间看看代码,并试图找出什么是错误的,那将是伟大的,或者如果有人有类似的问题,只是把它扔出去,这样我就可以检查我是否做了同样的事情:)

致以最亲切的问候。菲利普

(抱歉我的英语不好,我来自瑞典,很着急…)

- my .m file

#import "FoodTableViewController.h"
@interface FoodTableViewController ()
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property(nonatomic)NSMutableArray *foodNames;
@end
@implementation FoodTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.foodNames = [@[]mutableCopy];
    self.searchBar.delegate = self;

    // 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;
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchbar
{
    NSString *urlString = [NSString stringWithFormat:@"http://matapi.se/foodstuff?query=%@",self.searchBar.text];
    NSURL *URL = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    NSURLSession *session = [NSURLSession sharedSession];

    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSError *parseError;
        NSArray *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&parseError];
        dispatch_async(dispatch_get_main_queue(),^{
            for(int i=0;i<json.count;i++){

                NSString *foodName = json[i][@"name"];
                [self.foodNames addObject:foodName];
                NSLog(@"Added: %@",foodName);
                NSLog(@"FOODLIST LENGTH: %d",self.foodNames.count);
            }
        });
    }];

    [task resume];
    [self.tableView reloadData];
}
#pragma mark - Table view data source
- (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.foodNames.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    if(self.foodNames[indexPath.row]){
        NSLog(@"Cell text %@",self.foodNames[indexPath.row]);
        cell.textLabel.text = self.foodNames[indexPath.row];
    }else{
        cell.textLabel.text = @"Loading..";
    }

    return cell;
}
@end

- my .h file

#import <UIKit/UIKit.h>
@interface FoodTableViewController : UITableViewController<UISearchBarDelegate>
@end

dataTaskWithRequest:...是一个异步方法,这意味着在调用reloadData时结果不可用。在将所有数据添加到数组之后,需要添加该调用。部分…

        for(int i=0;i<json.count;i++){

            NSString *foodName = json[i][@"name"];
            [self.foodNames addObject:foodName];
            NSLog(@"Added: %@",foodName);
            NSLog(@"FOODLIST LENGTH: %d",self.foodNames.count);
        }
        [self.tableView reloadData];