我已将delegateand设置datasource为File's Owner,文件中的插座已正确设置xib。现在对于.h文件:
@interface ProductsViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>{
IBOutlet UITableView *objTableView;
}
@property(nonatomic,strong)IBOutlet UITableView *objTableView;
在.m文件中:
NSLog(@"%@",self.objTableView);
[self.objTableView reloadData];
第一次,self.objTableView正确设置:
NSLog(@"%@",self.objTableView);
给出:
<UITableView: 0x1d9a5800; frame = (4 54; 532 660); clipsToBounds = YES; autoresize = W+H;
但是下一次我得到了一个(null)表格视图对象,所以reloadData不会刷新表格视图。如何解决这个问题,请提前谢谢。
编辑:
我正在使用如下Afnetworking方法JSONRequestOperationWithRequest:
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON){
[SVProgressHUD dismiss];
//Get the response from the server
//And then refresh the tableview
[self.objTableView reloadData];//I shouldn't put this here, it should be in the main thread
}failure:^(NSURLRequest *request, NSHTTPURLResponse *response,NSError *error, id JSON){
[SVProgressHUD dismiss];
//Alert the error message
}];
[operation start];
[SVProgressHUD showWithStatus:@"Searching for products, please wait.."];
实际上,JSONRequestOperationWithRequest异步运行,所以不在主线程中,但是它变成了UI更新应该在主线程中完成,所以我需要[self.objTableView reloadData];在该方法之外删除。但是哪里?如何确保JSONRequestOperationWithRequest完成后在主线程中运行它?