从 View Did Load 中检索到的数据创建 table 视图控制器
Creating table view controllers from data retrieved in View Did Load
您好,我是创建 Table 视图控制器的新手。我在尝试将通过 ViewDidLoad 部分中的 Json 检索到的数据加载到 table 视图控制器时遇到困难。
我注意到的第一件事是,我最终放置我的 json 数据(dataArray)的数组在:numberOfRowsInSection 中不可用,所以我在 viewDidLoad 之外声明了它,但现在看到当我调试数组时它是空的到 numberOfRowsInSection。我事先测试了检索代码,所以我确信它能正常工作。
我有一些问题:
1. 为什么dataArray在其他方法中是空的?
2. 在 ViewDidLoad 中加载数据以在 table 视图中显示是否合适,或者我真的应该将此数据加载到其他地方更可见并可供 table 视图方法使用的地方吗?
代码如下:
NSArray *dataArray = nil;
- (void)viewDidLoad {
[super viewDidLoad];
dataArray = [[NSArray alloc] init];
// Set URL variable with Token
NSString *token = @"99fdf505547d607e65b8b8ff16113337";
NSString *teacherUrl = [NSString stringWithFormat: @"https://covle.com/apps/api/nextdoorteacher/teachers?t=%@",token];
// Set up the session configuration
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];
[[session dataTaskWithURL:[NSURL URLWithString:teacherUrl]
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
NSLog(@"response ==> %@", response);
if (!error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (httpResponse.statusCode == 200){
NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:&error];
NSLog(@" heres the serialised json ===> %@", jsonData);
// move serialised JSON into Array (as the data contained is in array)
dataArray=(NSArray*)[json数据复制];
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return dataArray.count;
NSLog(@"Array count is set to ===> %@", dataArray.count);
如果能帮助理解和解决这个问题,我们将不胜感激。
谢谢
为此
NSArray *dataArray=(NSArray*)[jsonData copy];
你正在创建一个名为 dataArray 的新局部变量,它与你在 viewDidLoad 中分配的全局 dataArray 不同,所以只写这个
dataArray = (NSArray*)[jsonData copy];
并且您将在 numberOfRowsInSection
中获得预期的数组计数
编辑:
在 dataArray = (NSArray*)[jsonData copy]
之后调用 [self.tableview reloadData]
以调用 tableview 委托方法再次触发!
您好,我是创建 Table 视图控制器的新手。我在尝试将通过 ViewDidLoad 部分中的 Json 检索到的数据加载到 table 视图控制器时遇到困难。 我注意到的第一件事是,我最终放置我的 json 数据(dataArray)的数组在:numberOfRowsInSection 中不可用,所以我在 viewDidLoad 之外声明了它,但现在看到当我调试数组时它是空的到 numberOfRowsInSection。我事先测试了检索代码,所以我确信它能正常工作。 我有一些问题: 1. 为什么dataArray在其他方法中是空的? 2. 在 ViewDidLoad 中加载数据以在 table 视图中显示是否合适,或者我真的应该将此数据加载到其他地方更可见并可供 table 视图方法使用的地方吗?
代码如下:
NSArray *dataArray = nil;
- (void)viewDidLoad {
[super viewDidLoad];
dataArray = [[NSArray alloc] init];
// Set URL variable with Token
NSString *token = @"99fdf505547d607e65b8b8ff16113337";
NSString *teacherUrl = [NSString stringWithFormat: @"https://covle.com/apps/api/nextdoorteacher/teachers?t=%@",token];
// Set up the session configuration
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];
[[session dataTaskWithURL:[NSURL URLWithString:teacherUrl]
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
NSLog(@"response ==> %@", response);
if (!error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (httpResponse.statusCode == 200){
NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:&error];
NSLog(@" heres the serialised json ===> %@", jsonData);
// move serialised JSON into Array (as the data contained is in array)
dataArray=(NSArray*)[json数据复制];
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return dataArray.count;
NSLog(@"Array count is set to ===> %@", dataArray.count);
如果能帮助理解和解决这个问题,我们将不胜感激。 谢谢
为此
NSArray *dataArray=(NSArray*)[jsonData copy];
你正在创建一个名为 dataArray 的新局部变量,它与你在 viewDidLoad 中分配的全局 dataArray 不同,所以只写这个
dataArray = (NSArray*)[jsonData copy];
并且您将在 numberOfRowsInSection
编辑:
在 dataArray = (NSArray*)[jsonData copy]
之后调用 [self.tableview reloadData]
以调用 tableview 委托方法再次触发!