objective-c – 解析后在单独的类中的UITableView委托和数据源

前端之家收集整理的这篇文章主要介绍了objective-c – 解析后在单独的类中的UITableView委托和数据源前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要从一个单独的类设置我的UITableView委托和数据源(通过方法调用解析后的数据就绪),但每次我的表都是emtpy时.我正在使用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,1,1) animated:YES];

                                               }
                                               failure:^(NSURLRequest *request,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测试它),但我的表是空的.你能帮我吗?

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

解决方法

从哪里/如何在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文件中创建一个属性父类

@property(nonatomic,retain) id parent;

然后将HomeController设置为HomeController viewDiDLoad中的委托

tableController.parent = self.

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

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

希望这能解决问题.

猜你在找的Xcode相关文章