目前,我在视图控制器旁边有我的JSON数据检索代码,一切正常。当我向我的应用程序添加更多功能时,我意识到我应该有一个单独的类,其工作是发出数据请求,以便其他viewControllers可以请求数据,而无需在每个视图控制器中重复执行代码。
将数据请求代码移动到新类后,我无法让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];
}
ViewView与TableView
- (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;
}
谢谢!
答案 0 :(得分:0)
最有可能的问题是,您正在使用"服务"初始化gameViewController。类。你正在调用init方法,你能告诉我们那段代码吗?你在那里初始化表视图?设置它的代理和数据源?或者你在使用故事板吗?
我的建议是不要从服务类初始化视图控制器。 您的UIViewController应该调用此类来获取数据,然后使用委托模式在服务器的数据可用时通知视图控制器。
如果您需要一个例子,请告诉我。
使用示例代码编辑:
在您的"新课程代码" class,在头文件(.h)中声明一个协议:
@protocol SomeClassDelegate
- (void)didReceiveGameData:(NSArray *)gameData;
@end
@interface NewClassCode : NSObject
@property (nonatomic, strong) id<SomeClassDelegate> delegate;
@end
在您的&#34;新课程代码&#34;实施(.m):
- (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];
}
}
然后在UIViewController中使用你的&#34;新类代码&#34;类:
声明UIViewController实现您刚刚定义的协议
@interface NewClassCode () <SomeClassDelegate>
实现接口
中定义的方法- (void)didReceiveGameData:(NSArray *)gameData {
self.gameData = gameData;
[self.tableView reloadData]
}
将您的视图控制器设置为服务类的委托(您称之为新类代码):
newClassCode.delegate = self;
这基本上是委托模式,您可以在互联网上搜索更多细节,但这个例子应该让您入门。基本上,您的视图控制器要求服务类执行操作并在完成后通知它。你也应该为错误案例做类似的事情。 另一个很好的方法是使用块,或使用已经使用块的AFNetworking。