TableView上没有填充数据

时间:2014-10-10 05:03:13

标签: ios uitableview

我从网站上获取数据并加载到tableViewController上。 Tableviewcontroller位于tabbarcontroller中。每当我点击tabbar时,都不会填充tableview数据。但是,一旦我单击其他viewcontrollers,然后再次单击tableviewcontroller,就会填充数据。

#import "GetBookViewController.h"
#import "AFNetworking.h"

@interface GetBookViewController ()

@end

@implementation GetBookViewController
@synthesize booksArray;
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

}

-(void)viewWillAppear:(BOOL)animated
{

    [self loadData];

}

-(void)viewDidAppear:(BOOL)animated
{

    [self.tableView reloadData];


}

-(void) loadData
{

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:@"http://XXXXXX.com/coursera/books.php" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
        if ([[responseObject valueForKey:@"status"] isEqualToString:@"success"]) {
            int count = [[responseObject valueForKey:@"total"] integerValue];
            NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:count];
            for (int i = 1; i <= count; i++) {
                NSString *obj = [NSString stringWithFormat:@"%i", i];
                [array addObject:[responseObject objectForKey:obj]];
            }
            booksArray = array;
            for (id obj in booksArray) {
                NSLog(@"%@", [obj valueForKey:@"title"]);
            }
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#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 [booksArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    UILabel* label = (UILabel*)[cell viewWithTag:100];
    NSString *title = [[booksArray objectAtIndex:indexPath.item] valueForKey:@"title"];
    label.text = title;

    return cell;
}

1 个答案:

答案 0 :(得分:1)

一旦收到来自网络的响应并填充阵列,您没有做任何事情?

您需要做的是通知表视图,它需要再次查询其数据源以刷新其值。一旦你拥有了你的数组,只需在你的表视图上调用reloadData即可:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:@"http://ilyasuyanik.com/coursera/books.php" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
    if ([[responseObject valueForKey:@"status"] isEqualToString:@"success"]) {
        int count = [[responseObject valueForKey:@"total"] integerValue];
        NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:count];
        for (int i = 1; i <= count; i++) {
            NSString *obj = [NSString stringWithFormat:@"%i", i];
            [array addObject:[responseObject objectForKey:obj]];
        }
        dispatch_async(dispatch_get_main_queue,^{
            booksArray = array;
            for (id obj in booksArray) {
                NSLog(@"%@", [obj valueForKey:@"title"]);
            }
           //now you can update your table view
           [self.tableView reloadData];
        });
    }
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];