Web api和tableView问题

时间:2014-05-22 22:42:48

标签: ios objective-c uitableview asp.net-web-api

我是Objective C和Xcode的新手,但我每天都在学习一点!:) 我正在尝试构建一个将在我的tableview中显示webb api的应用程序,但它没有显示..当我NSLog它时,它表明我的搜索工作,它得到了我正在寻找的数据,但它不会显示在我的tableView中不幸..

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

致以最诚挚的问候。菲利普

(抱歉我的英语不好,我来自瑞典,匆忙......)

- 我的.m文件

#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

- 我的.h文件

#import <UIKit/UIKit.h>

@interface FoodTableViewController : UITableViewController<UISearchBarDelegate>

@end

1 个答案:

答案 0 :(得分:0)

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];