Ios 未在TableView上填充数据
我正在从网站获取数据并加载到tableViewController上。Tableviewcontroller位于tabbarcontroller内部。每当我在选项卡栏上单击KED时,都不会填充tableview数据。但是,一旦我单击其他ViewController,然后再次单击tableviewcontroller,就会填充数据Ios 未在TableView上填充数据,ios,uitableview,Ios,Uitableview,我正在从网站获取数据并加载到tableViewController上。Tableviewcontroller位于tabbarcontroller内部。每当我在选项卡栏上单击KED时,都不会填充tableview数据。但是,一旦我单击其他ViewController,然后再次单击tableviewcontroller,就会填充数据 #import "GetBookViewController.h" #import "AFNetworking.h" @interface GetBookViewCo
#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;
}
#导入“GetBookViewController.h”
#导入“AFNetworking.h”
@接口GetBookViewController()
@结束
@GetBookViewController的实现
@合成阵列;
-(id)initWithStyle:(UITableViewStyle)样式
{
self=[super initWithStyle:style];
如果(自我){
//自定义初始化
}
回归自我;
}
-(无效)viewDidLoad
{
[超级视图下载];
}
-(无效)视图将显示:(BOOL)动画
{
[自动加载数据];
}
-(无效)视图显示:(BOOL)动画
{
[self.tableView重载数据];
}
-(void)加载数据
{
AFHTTPRequestOperationManager*manager=[AFHTTPRequestOperationManager];
[经理职务:@”http://XXXXXX.com/coursera/books.php参数:无成功:^(AFHTTPRequestOperation*操作,id响应对象){
NSLog(@“JSON:%@”,responseObject);
如果([[responseObject valueForKey:@“status”]IseQualString:@“success”]){
int count=[[responseObject valueForKey:@“total”]integerValue];
NSMutableArray*数组=[[NSMutableArray alloc]initWithCapacity:count];
对于(int i=1;i一旦收到来自网络的响应并填充数组,您就什么都不做了
您需要做的是通知表视图它需要再次查询其数据源以刷新其值。获得数组后,只需在表视图上调用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);
}];
AFHTTPRequestOperationManager*manager=[AFHTTPRequestOperationManager];
[经理职务:@”http://ilyasuyanik.com/coursera/books.php参数:无成功:^(AFHTTPRequestOperation*操作,id响应对象){
NSLog(@“JSON:%@”,responseObject);
如果([[responseObject valueForKey:@“status”]IseQualString:@“success”]){
int count=[[responseObject valueForKey:@“total”]integerValue];
NSMutableArray*数组=[[NSMutableArray alloc]initWithCapacity:count];
对于(int i=1;i是,但您没有调用重载数据,请参阅我的解决方案:)太好了!请更新代码dispatch\u async(dispatch\u get\u main\u queue())的以下行^{我认为您不需要dispatch_async。我很确定成功块是在主线程上调用的。@rdelmar这是一个不安全的假设,将来很容易改变。如果没有设置回调队列,管理器默认使用主线程,但安全没有坏处。