xcode 解析后在单独的类中的 UITableView 委托和数据源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15407844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UITableView delegate and datasource in a separate class after parsing
提问by Huxley
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:
我需要从一个单独的类中设置我的 UITableView 委托和数据源(在方法调用解析后准备好数据),但每次我的表都是空的。我正在使用 ARC,这是简化的代码:
//HomeViewController.h
#import <UIKit/UIKit.h>
#import "TableController.h"
@interface HomeViewController : UIViewController {
IBOutlet UITableView *table;
TableController *tableController;
}
@end
and
和
//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];
}
and
和
// 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
and
和
//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];
}
and, obviously, still in TableController.m, all the classic UITableView delegate methods:
而且,显然,仍然在 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
}
Well, the parse is perfect (I can test it with a NSLog), but my tableis empty. Can u help me?
好吧,解析是完美的(我可以用 NSLog 测试它),但我的表是空的。你能帮我吗?
EDIT: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.
编辑:在我的代码中,loadTable
解析方法是异步的,因此使用正确的数据源和委托加载表,但在解析所有数据之前;事实上,如果我设置了一个固定的 numberOfRows 然后 SCROLL TABLE 我可以看到填充的所有行。但是,当我加载 HomeViewController 时,*table 仍然是空的。
回答by arun.s
From where/How you are setting UITableView *tableSource
object in your TableController ?
您从何处/如何UITableView *tableSource
在 TableController中设置对象?
Also try reloading tableview in main thread.
还可以尝试在主线程中重新加载 tableview。
Edit
编辑
Change these in your HomeController viewDidLoad
在 HomeController viewDidLoad 中更改这些
table.dataSource = tableController.tableSource.dataSource;
table.delegate = tableController.tableSource.delegate;
to
到
table.dataSource = tableController;
table.delegate = tableController;
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)!!!
一旦你得到响应,也将 set HomeController
class 作为TableController
& 的代表。调用一个方法HomeController
来重新加载tableview(在主线程中)!!!
For that first create a property parent
in your TableController .h file
like this
对于首先创建一个property parent
你TableController .h file
喜欢这个
@property(nonatomic,retain) id parent;
Then set the HomeController as the delegate from HomeController
viewDiDLoad like
然后将 HomeController 设置为HomeController
viewDiDLoad的委托,如
tableController.parent = self
.
tableController.parent = self
.
Once you get response in completion block call,
一旦你在完成块调用中得到响应,
[self.parent reloadTableView];
// reloadTableView will be a function in the HomeController
that is having [self.table reloadData]
.
[self.parent reloadTableView];
// reloadTableView 将HomeController
是具有[self.table reloadData]
.
Hope this will fix the issue.
希望这能解决问题。