【问题标题】:UITableView delegate and datasource in a separate class after parsing解析后UITableView委托和数据源在一个单独的类中
【发布时间】:2013-03-14 11:23:05
【问题描述】:

我需要从一个单独的类设置我的 UITableView 委托和数据源(通过方法调用解析后准备好数据),但每次我的表都是空的。我正在使用 ARC,这是简化的代码:

//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 委托方法:

- (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 测试它),但是我的 table 是空的。你能帮帮我吗?

编辑:在我的代码loadTable 中,解析方法是异步的,因此使用正确的数据源和委托进行表加载,但在解析所有数据之前;事实上,如果我设置一个固定的 numberOfRows 然后 SCROLL TABLE 我可以看到所有填充的行。但是,当我加载 HomeViewController 时,*table 仍然是 EMPTY。

【问题讨论】:

  • numberOfSectionsInTableView 是否被调用?如果没有,您可能没有正确设置 dataSource 属性,或者 reloadData 从未执行过。 (tableController.tableSource.dataSource 真的返回什么吗?)
  • 它全部被调用,如果我把一个 UITableView INTO TableController(像一个 ViewController)它不是空的,但 tableController.tableSource.dataSource 实际上返回 NULL
  • 你试过了吗 table = tableController.tableSource; ??
  • @Jeremy 我无法尝试,我收到错误“无法识别的选择器已发送到实例..:”
  • 嗯好吧,我不知道。您是否将 xib 文件/情节提要视图中的 tableview 链接到头文件中的 tableview?您是否还将 xib 文件/情节提要视图中的委托/数据源链接到视图控制器?

标签: objective-c xcode uitableview


【解决方案1】:

从哪里/如何在 TableController 中设置 UITableView *tableSource 对象?

还可以尝试在主线程中重新加载 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 file 中创建一个property parent,如下所示

@property(nonatomic,retain) id parent;

然后将 HomeController 设置为来自 HomeController viewDiDLoad 的委托,如

tableController.parent = self.

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

[self.parent reloadTableView]; // reloadTableView 将是具有[self.table reloadData]HomeController 中的一个函数。

希望这能解决问题。

【讨论】:

  • 我尝试在 HomeViewController ViewDidLoad 中添加此代码:NSLog(@"TABLE: %@",tableController.tableSource); 结果为 NULL
  • 谢谢你,@arun.s,你明白了!在我的代码中,loadTable 解析方法是异步的,因此我的表加载了正确的数据源和委托,但在解析所有数据之前;事实上,如果我滚动表,我可以看到填充的所有行。不幸的是,您的代码不起作用,表格仍然是空的......
  • 但是当你开始解析的时候你把 userInteractionEnabled 设置为 NO 吗? .我还用虚拟数据源测试了代码(你的和我的),它工作正常。
  • 是的,但 userInteractionEnabled 可以是 YES 或 NO,问题是我无法在解析结束后立即调用 [table reloadData]。事实上,如果我用方法(或 SCROLL)表手动重新加载是完美的..
  • 为此我们将 HomeController 设置为 TableController 的委托,以便您可以像这样调用 [self.parent reloadTableView];并且 HomeController 将具有 reloadTableView 功能,就像我在 EDITED POST 中提到的那样。
猜你喜欢
  • 2016-02-29
  • 1970-01-01
  • 2012-07-01
  • 2016-07-27
  • 1970-01-01
  • 2015-11-05
  • 2018-01-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多