Hux*_*ley 3 xcode objective-c uitableview
我需要从一个单独的类设置我的UITableView委托和数据源(通过方法调用解析后的数据就绪),但每次我的表都是emtpy时.我正在使用ARC,这是简化的代码:
//HomeViewController.h
#import <UIKit/UIKit.h>
#import "TableController.h"
@interface HomeViewController : UIViewController {
IBOutlet UITableView *table;
TableController *tableController;
}
@end
Run Code Online (Sandbox Code Playgroud)
和
//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];
}
Run Code Online (Sandbox Code Playgroud)
和
// 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
Run Code Online (Sandbox Code Playgroud)
和
//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];
}
Run Code Online (Sandbox Code Playgroud)
而且,显然,仍然在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
}
Run Code Online (Sandbox Code Playgroud)
好吧,解析是完美的(我可以用NSLog测试它),但我的表是空的.你能帮我吗?
编辑:在我的代码中,loadTable
解析方法是异步的,所以表加载了正确的数据源和委托,但是在解析所有数据之前; 实际上如果我设置了一个固定的numberOfRows然后SCROLL TABLE我可以看到填充的所有行.但是,当我加载HomeViewController时,*表仍然是EMPTY.
从哪里/如何UITableView *tableSource
在TableController 中设置对象?
还尝试在主线程中重新加载tableview.
编辑
在HomeController viewDidLoad中更改这些
table.dataSource = tableController.tableSource.dataSource;
table.delegate = tableController.tableSource.delegate;
Run Code Online (Sandbox Code Playgroud)
至
table.dataSource = tableController;
table.delegate = tableController;
Run Code Online (Sandbox Code Playgroud)
一旦得到响应,也可以将set HomeController
class 设置为TableController
&的委托.调用一个方法HomeController
来重新加载tableview(在主线程中)!!!
为此,首先property parent
在你的TableController .h file
喜欢中创造一个
@property(nonatomic,retain) id parent;
Run Code Online (Sandbox Code Playgroud)
然后将HomeController设置为来自HomeController
viewDiDLoad 的委托
tableController.parent = self
.
一旦你在完成块调用中得到响应,
[self.parent reloadTableView];
// reloadTableView将在一个功能HomeController
是具有[self.table reloadData]
.
希望这能解决问题.
归档时间: |
|
查看次数: |
8363 次 |
最近记录: |