解析后在单独的类中的 UITableView 委托和数据源 [英] UITableView delegate and datasource in a separate class after parsing
问题描述
我需要从一个单独的类中设置我的 UITableView 委托和数据源(在方法调用解析后准备好数据),但每次我的表都是空的.我正在使用 ARC,这是简化的代码:
I need to set my UITableView delegate and datasource from a separate class (data ready after parsing called by a method), but every time my table is emtpy. I'm using ARC and this is simplified code:
//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, 0, 1, 1) animated:YES];
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"%@", [error userInfo]);
}];
[parseOperation start];
[tableSource setUserInteractionEnabled:NO];
}
显然,仍然在 TableController.m 中,所有经典的 UITableView 委托方法:
and, obviously, still in TableController.m, all the classic UITableView delegate methods:
- (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 测试它),但是我的表是空的.你能帮我吗?
Well, the parse is perfect (I can test it with a NSLog), but my table is empty. Can u help me?
在我的代码中,loadTable
解析方法是异步的,因此使用正确的数据源和委托加载表,但在解析所有数据之前;事实上,如果我设置了一个固定的 numberOfRows 然后 SCROLL TABLE 我可以看到填充的所有行.但是,当我加载 HomeViewController 时,*table 仍然是空的.
In my code loadTable
parsing method is asynchronous, so table load with right datasource and delegate but BEFORE all data is parsed; in fact IF I SET a fixed numberOfRows and then SCROLL TABLE I can see all the rows populated. But, when I load HomeViewController, *table is still EMPTY.
推荐答案
从哪里/如何在 TableController 中设置 UITableView *tableSource
对象?
From where/How you are setting UITableView *tableSource
object in your TableController ?
还尝试在主线程中重新加载 tableview.
Also try reloading tableview in main thread.
编辑
在 HomeController viewDidLoad 中更改这些
Change these in your HomeController viewDidLoad
table.dataSource = tableController.tableSource.dataSource;
table.delegate = tableController.tableSource.delegate;
到
table.dataSource = tableController;
table.delegate = tableController;
同时设置 HomeController
类作为 TableController
的委托 &一旦你得到回应.在 HomeController
中调用一个方法来重新加载 tableview(在主线程中)!!!
Also get set HomeController
class as delegate of TableController
& once you get the response . Call a method in HomeController
to reload the tableview (in main thread)!!!
首先在你的 TableController .h 文件
中创建一个 property parent
像这样
For that first create a property parent
in your TableController .h file
like this
@property(nonatomic,retain) id parent;
然后将 HomeController 设置为来自 HomeController
viewDiDLoad 的委托,如
Then set the HomeController as the delegate from HomeController
viewDiDLoad like
tableController.parent = self
.
一旦你在完成块调用中得到响应,
Once you get response in completion block call,
[self.parent reloadTableView];
//reloadTableView 将是 HomeController
中的一个函数,它有 [self.table reloadData]
.
[self.parent reloadTableView];
// reloadTableView will be a function in the HomeController
that is having [self.table reloadData]
.
希望这能解决问题.
这篇关于解析后在单独的类中的 UITableView 委托和数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!