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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:58:58  来源:igfitidea点击:

UITableView delegate and datasource in a separate class after parsing

objective-cxcodeuitableview

提问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 loadTableparsing 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 *tableSourceobject 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 HomeControllerclass as delegate of TableController& once you get the response . Call a method in HomeControllerto reload the tableview (in main thread)!!!

一旦你得到响应,也将 set HomeControllerclass 作为TableController& 的代表。调用一个方法HomeController来重新加载tableview(在主线程中)!!!

For that first create a property parentin your TableController .h filelike this

对于首先创建一个property parentTableController .h file喜欢这个

@property(nonatomic,retain) id parent;

Then set the HomeController as the delegate from HomeControllerviewDiDLoad like

然后将 HomeController 设置为HomeControllerviewDiDLoad的委托,如

tableController.parent = self.

tableController.parent = self.

Once you get response in completion block call,

一旦你在完成块调用中得到响应,

[self.parent reloadTableView];// reloadTableView will be a function in the HomeControllerthat is having [self.table reloadData].

[self.parent reloadTableView];// reloadTableView 将HomeController是具有[self.table reloadData].

Hope this will fix the issue.

希望这能解决问题。