大佬教程收集整理的这篇文章主要介绍了objective-c – 解析后在单独的类中的UITableView委托和数据源,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
//HomeViewController.h #import <UIKit/UIKit.h> #import "TableController.h" @interface HomeViewController : UIViewController { IBOutlet UITableView *table; TableController *tableController; } @end
和
//HomeViewController.m #import "HomeViewController.h" @interface HomeViewController () @end @implementation HomeViewController - (void)viewDidLoad { [super viewDidLoad]; tableController = [[TableController alloc] init]; table.datasource = tableController.tablesource.datasource; table.delegate = tableController.tablesource.delegate; [tableController loadTable]; // HERE I CALL LOADTABLE FROM TABLECONTROLLER CLASS TO PARSE DATA AND POPULATE UITABLEVIEW [table reloadData]; }
和
// TableController.h #import <UIKit/UIKit.h> @interface TableController : NSObject <UITableViewDelegate,UITableViewDatasource> { UITableView *tablesource; // a lot of NSMutableArray to parse my data } - (void)loadTable; @property (nonatomic,strong) UITableView *tablesource; @end
和
//TableController.m #import "TableController.h" #import "AFNetworking.h" @interface TableController () @end @implementation TableController @synthesize tablesource; - (void)loadTable { NSURL *parseURL = // remote URL to parse Data NSURLrequest *request = [NSURLrequest requestWithURL:parseURL]; AFJSONrequestOperation *parSEOperation = [AFJSONrequestOperation JSONrequestOperationWithrequest:request success:^(NSURLrequest *request,NShttpURLResponse *response,id JSON) { // code to parse Data and NSLog to test operation [tablesource reloadData]; [tablesource setUserInteractionEnabled:YES]; [tablesource scrollRectToVisible:CGRectMake(0,1,1) animated:YES]; } failure:^(NSURLrequest *request,NSError *error,id JSON) { NSLog(@"%@",[error userInfo]); }]; [parSEOperation start]; [tablesource setUserInteractionEnabled:NO]; }
而且,显然,仍然在TableController.m中,所有经典的UITableView委托方法:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // my code } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // my code } - (CGFloat)tableView:(UITableView *)tableView heightForRowATindexPath:(NSIndexPath *)indexPath { // my code } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { // my code } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowATindexPath:(NSIndexPath *)indexPath{ // my code } - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowATindexPath:(NSIndexPath *)indexPath { // my code } - (void)tableView:(UITableView *)tableView didSELEctRowATindexPath:(NSIndexPath *)indexPath { // my code }
好吧,解析是完美的(我可以用NSLog测试它),但我的表是空的.你能帮我吗?
编辑:在我的代码中,loadTable解析方法是异步的,所以表加载了正确的数据源和委托但是在解析所有数据之前;实际上如果我设置了一个固定的numberOfRows然后SCROLL TABLE我可以看到填充的所有行.但是,当我加载HomeViewController时,*表仍然是EMPTY.
还尝试在主线程中重新加载tableview.
编辑
在HomeController viewDidLoad中更改这些
table.datasource = tableController.tablesource.datasource; table.delegate = tableController.tablesource.delegate;
至
table.datasource = tableController; table.delegate = tableController;
同时将HomeController类设置为TableController&的委托.一旦得到回复.在HomeController中调用一个方法来重新加载tableview(在主线程中)!
为此,首先在TableController .h文件中创建一个属性父类
@property(nonatomic,retain) id parent;
然后将HomeController设置为HomeController viewDiDLoad中的委托
tableController.parent = self.
一旦你在完成块调用中得到响应,
[self.parent reloadTableView]; // reloadTableView将是HomeController中具有[self.table reloadData]的函数.
希望这能解决问题.
以上是大佬教程为你收集整理的objective-c – 解析后在单独的类中的UITableView委托和数据源全部内容,希望文章能够帮你解决objective-c – 解析后在单独的类中的UITableView委托和数据源所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。