Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从其他类将数据加载到UITableView时出现问题_Ios_Objective C - Fatal编程技术网

Ios 从其他类将数据加载到UITableView时出现问题

Ios 从其他类将数据加载到UITableView时出现问题,ios,objective-c,Ios,Objective C,目前,我在视图控制器中有JSON数据检索代码,一切正常。当我向我的应用程序添加更多功能时,我意识到我应该有一个单独的类,它的工作是发出数据请求,这样其他视图控制器就可以请求数据,而不必通过每个视图控制器重复代码 在我将数据请求代码移动到一个新类中之后,我很难让tableView从该类加载数据。数据连接正在工作,因为我有一个NSLog,显示请求的内容,但数据没有填充到我的tableView中 新类代码 - (void)connection:(NSURLConnection *)connection

目前,我在视图控制器中有JSON数据检索代码,一切正常。当我向我的应用程序添加更多功能时,我意识到我应该有一个单独的类,它的工作是发出数据请求,这样其他视图控制器就可以请求数据,而不必通过每个视图控制器重复代码

在我将数据请求代码移动到一个新类中之后,我很难让tableView从该类加载数据。数据连接正在工作,因为我有一个NSLog,显示请求的内容,但数据没有填充到我的tableView中

新类代码

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
// A response has been received, this is where we initialize the instance var you created
// so that we can append data to it in the didReceiveData method
// Furthermore, this method is called each time there is a redirect so reinitializing it
// also serves to clear it
_searchResponseData = [[NSMutableData alloc] init];

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Append the new data to the instance variable you declared
[_searchResponseData appendData:data];
NSLog(@"Recieved Data");

}

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
              willCacheResponse:(NSCachedURLResponse*)cachedResponse {
// Return nil to indicate not necessary to store a cached response for this connection
return nil;
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// The request is complete and data has been received
// You can parse the stuff in your instance variable now

gameViewController *gameTableClass = [[gameViewController alloc] init];

NSError *error;
NSDictionary *Data = [NSJSONSerialization JSONObjectWithData:_searchResponseData options:0 error:&error];
NSLog(@"%@", Data);
NSDictionary *parseData = [archiveData objectForKey:@"games"];
gameDataArray  = [parseData objectForKey:@"game"];
gameTableClass.gameSearchArray = gameDataArray;
[gameTableClass.tableView reloadData];
[[[gameTableClass searchDisplayController] searchResultsTableView] performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];

}


- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// The request has failed for some reason!
// Check the error var
NSLog(@"%@", error);
}

-(void)newConnectionToServer:(NSURLRequest *)searchRequest {
[NSURLConnection connectionWithRequest:searchRequest delegate:self];
}
带有TableView的ViewController

- (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 [gameSearchArray count];


}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//static NSString *cellIdentifier = @"Cell";
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell==nil) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];

}


NSDictionary *tempDictionary = [gameSearchArray objectAtIndex:indexPath.row];
cell.textLabel.text = [tempDictionary valueForKey:@"title"];
cell.detailTextLabel.text = [tempDictionary valueForKey:@"system_title"];


return cell;
}

谢谢

问题很可能是您正在从“服务”类初始化gameViewController。你在调用init方法,能给我们看一下代码吗?您正在初始化那里的表视图吗?设置它的委托和数据源?或者你在使用故事板

我的建议是不要从服务类初始化视图控制器。 UIViewController应该调用此类以获取数据,然后使用委托模式在服务器数据可用时通知视图控制器

如果你需要一个例子,请告诉我

使用示例代码编辑:

在“新类代码”类中,在头文件(.h)中声明协议:

然后在使用“新类代码”类的UIViewController中:

  • 声明UIViewController以实现刚才定义的协议

    @interface NewClassCode () <SomeClassDelegate>
    
  • 将视图控制器设置为服务类的代理(您称之为新类代码):

  • 这基本上是委托模式,您可以在internet上搜索更多详细信息,但是这个示例应该可以帮助您开始。基本上,视图控制器要求服务类执行操作,并在操作完成时通知它。您还应该为错误案例执行类似的操作。
    另一种很好的方法是使用块,或者使用已经在使用块的AFNetworking。

    R谢谢!我最初把一切都安排在故事板上。因此,表视图数据源和委托是gameViewController中的表视图。如果你能举个例子,那就太好了。谢谢此行,gameViewController*gameTableClass=[[gameViewController alloc]init];,正在创建gameViewController的新实例,而不是您在故事板中创建的实例,因此,您在屏幕上看到的实例当然不会更新。你想遵循的模式是Matias在他的回答中建议的模式。@rdelmar我明白了,我将寻找一个如何实现它的示例。谢谢
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
        NSError *error;
        NSDictionary *Data = [NSJSONSerialization JSONObjectWithData:_searchResponseData options:0 error:&error];
        NSLog(@"%@", Data);
        NSDictionary *parseData = [archiveData objectForKey:@"games"];
        gameDataArray  = [parseData objectForKey:@"game"];
        if (_delegate) {
            [_delegate didReceiveGameData:gameDataArray];
        }
    }
    
    @interface NewClassCode () <SomeClassDelegate>
    
    - (void)didReceiveGameData:(NSArray *)gameData {
        self.gameData = gameData;
        [self.tableView reloadData]
    }
    
    newClassCode.delegate = self;