Objective c 解析后,UITableView委托和数据源位于单独的类中
我需要从一个单独的类中设置UITableView委托和数据源(通过方法调用解析后数据就绪),但每次我的表都是emtpy。我使用的是ARC,这是简化的代码:Objective c 解析后,UITableView委托和数据源位于单独的类中,objective-c,xcode,uitableview,Objective C,Xcode,Uitableview,我需要从一个单独的类中设置UITableView委托和数据源(通过方法调用解析后数据就绪),但每次我的表都是emtpy。我使用的是ARC,这是简化的代码: //HomeViewController.h #import <UIKit/UIKit.h> #import "TableController.h" @interface HomeViewController : UIViewController { IBOutlet UITableView *table;
//HomeViewController.h
#import <UIKit/UIKit.h>
#import "TableController.h"
@interface HomeViewController : UIViewController {
IBOutlet UITableView *table;
TableController *tableController;
}
@end
及
显然,在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测试它),但是我的表是空的。你能帮我吗
EDIT:在我的代码中
loadTable
解析方法是异步的,所以在解析所有数据之前,使用正确的数据源和委托加载表;事实上,如果我设置一个固定的行数,然后滚动表格,我可以看到所有填充的行。但是,当我加载HomeViewController时,*表仍然是空的。从哪里/如何设置TableController中的UITableView*tableSource
对象
还可以尝试在主线程中重新加载tableview
编辑
在HomeController视图中更改这些设置
table.dataSource = tableController.tableSource.dataSource;
table.delegate = tableController.tableSource.delegate;
到
获得响应后,还可以将setHomeController
类作为TableController
的代理。在HomeController
中调用一个方法来重新加载tableview(在主线程中)
为此,首先在TableController.h文件中创建一个属性父级
,如下所示
@property(nonatomic,retain) id parent;
然后将HomeController设置为来自HomeController
viewDiDLoad的委托
tableController.parent=self
一旦在完成块调用中得到响应
[self.parent reloadTableView]代码>//reloadTableView将是HomeController
中具有[self.table reloadData]
的函数
希望这能解决问题。是否调用numberOfSectionsInTableView?否则,您可能无法正确设置dataSource属性,或者从未执行过重新加载数据。(tableController.tableSource.dataSource是否实际返回任何内容?)如果我将UITableView放入tableController(如ViewController)中,它将被全部调用,它不是空的,但tableController.tableSource.dataSource实际返回Null您是否尝试过table=tableController.tableSource@Jeremy我无法尝试,我收到错误“无法识别的选择器发送到实例…”:“嗯,好的,我不知道。是否将xib文件/故事板视图中的tableview链接到头文件中的tableview?另外,您是否将xib文件/故事板视图中的代理/数据源链接到视图控制器?我已尝试在HomeViewController ViewDidLoad:NSLog(@“TABLE:%@”,tableController.tableSource)中添加此代码代码>结果为空谢谢,@arun.s,你明白了!在我的代码中,loadTable
解析方法是异步的,因此我的表在解析所有数据之前加载了正确的数据源和委托;事实上,如果我滚动表格,我可以看到所有填充的行。不幸的是,您的代码不起作用,表仍然是空的……但是您在开始解析时将userInteractionEnabled设置为“否”,对吗。此外,我还使用虚拟数据源测试了代码(您和我的组合),它运行良好。是的,但userInteractionEnabled可以是是或否,问题是我无法在解析结束后立即调用[表重载数据]
。事实上,如果我用一个方法(或滚动)手动重新加载表格是完美的..为此,我们将HomeController设置为TableController的代理,以便您可以像这样调用[self.parent reloadTableView];HomeController将具有重载TableView功能,就像我在编辑的帖子中提到的那样。
- (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
}
table.dataSource = tableController.tableSource.dataSource;
table.delegate = tableController.tableSource.delegate;
table.dataSource = tableController;
table.delegate = tableController;
@property(nonatomic,retain) id parent;