Ios 使用相同json提要的两个表视图
我有一个表格视图,当你按下一个单元格时,它会把你带到另一个表格视图。它使用相同的json提要,并将提要放入两个表的viewdidload方法中。当你按下进入下一个表格视图时,加载速度很慢,我猜是因为它再次加载了提要。我可以将json提要放在第一个表中,然后将其带到下一个表中,这样它就不需要再次加载它了吗?如果是这样,我该怎么做呢 如果有人能帮忙,非常感谢你的时间代码如下 视图没有加载Ios 使用相同json提要的两个表视图,ios,iphone,json,uitableview,Ios,Iphone,Json,Uitableview,我有一个表格视图,当你按下一个单元格时,它会把你带到另一个表格视图。它使用相同的json提要,并将提要放入两个表的viewdidload方法中。当你按下进入下一个表格视图时,加载速度很慢,我猜是因为它再次加载了提要。我可以将json提要放在第一个表中,然后将其带到下一个表中,这样它就不需要再次加载它了吗?如果是这样,我该怎么做呢 如果有人能帮忙,非常感谢你的时间代码如下 视图没有加载 - (void)viewDidLoad { [super viewDidLoad]; // Set T
- (void)viewDidLoad
{
[super viewDidLoad];
// Set Title
self.title = @"Authors";
dispatch_async(dispatch_get_main_queue(), ^{
// Update the UI
NSURL *blogURL = [NSURL URLWithString:@"http://www.how-e.co.uk/test.json"];
NSLog(@"BLOG URL > %@", blogURL);
NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];
NSLog(@"JSONDATA > %@", jsonData);
NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSLog(@"JSONDATA > %@", jsonData);
self.authors = [dataDictionary objectForKey:@"Root"];
self.searchResults = [NSMutableArray arrayWithCapacity:[authors count]];
[self.tableView reloadData];
});
链接到下一个表视图
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Initialize Books View Controller
MTBooksViewController *booksViewController = [[MTBooksViewController alloc] init];
if(tableView == self.searchDisplayController.searchResultsTableView) {
// Fetch and Set Author
NSDictionary *search = [searchResults objectAtIndex:[indexPath row]];
[booksViewController setAuthor:[search objectForKey:@"name"]];
}else {
NSDictionary *author = [authors objectAtIndex:[indexPath row]];
[booksViewController setAuthor:[author objectForKey:@"name"]];
}
// Push View Controller onto Navigation Stack
[self.navigationController pushViewController:booksViewController animated:YES];
}
第二桌
-(void)connect{
NSURL *blogURL = [NSURL URLWithString:@"http://www.how-e.co.uk/test.json"];
NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];
NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
// Update the UI
NSArray *authors = [dataDictionary objectForKey:@"Root"];
//NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Books" ofType:@"plist"];
//NSArray *authors = [NSArray arrayWithContentsOfFile:filePath];
for (int i = 0; i < [authors count]; i++) {
NSDictionary *authorDictionary = [authors objectAtIndex:i];
NSString *tempAuthor = [authorDictionary objectForKey:@"name"];
if ([tempAuthor isEqualToString:_author]) {
self.books = [authorDictionary objectForKey:@"Books"];
}
}
为什么要在第二个表中再次解析词典。创建自定义初始值设定项,如 第二页.h
@property (nonatomic ,retain) NSMutableArray *searchResultarray;
-(id)initWithSearchResults:(NSMutableArray*)searchResult;
并将接收到的数组用作中表的数据源
第二页
@sythesize searchResultarray= _searchResultarray;
-(id)initWithSearchResults:(NSMutableArray*)searchResult
{
if (self = [super init])
{
_searchResultarray= search;
}
}
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.searchResultarray count];
}
在第二个视图控制器中声明NSDictionary属性,并按如下方式从第一个视图传递NSDictionary:
- (void)viewDidLoad
{
[super viewDidLoad];
// Set Title
self.title = @"Authors";
dispatch_async(dispatch_get_main_queue(), ^{
// Update the UI
NSURL *blogURL = [NSURL URLWithString:@"http://www.how-e.co.uk/test.json"];
NSLog(@"BLOG URL > %@", blogURL);
NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];
NSLog(@"JSONDATA > %@", jsonData);
NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
//Here pass the data to the NSDictionary of the secondViewController
secondViewController.sameDataDictionary = [[NSMutableDictionary alloc]initWithDictionary:dataDictionary];
NSLog(@"JSONDATA > %@", jsonData);
self.authors = [dataDictionary objectForKey:@"Root"];
self.searchResults = [NSMutableArray arrayWithCapacity:[authors count]];
[self.tableView reloadData];
});
尝试将其添加到第二个.h@property NSDictionary*sameDataDictionary;但它在第一位表示secondViewController.sameDataDictionary=[[NSMutableDictionary alloc]initWithDictionary:dataDictionary];也就是说,在对象类型上找不到sameDataDictionary将导入“Second.h”添加到第一个view.m文件。我现在没有收到任何错误,但我没有得到任何结果,nslog结果为null,有什么建议吗?您在secondViewController中声明了属性吗<代码>@property(强,非原子)NSDictionary*sameDictionary代码>我有
MTBooksViewController*booksViewController;BookViewController.sameDataDictionary=[[NSDictionary alloc]initWithDictionary:dataDictionary]在第一个tableview.m中,然后在第二个.h中,我有@property(强,非原子)NSDictionary*sameDataDictionary在第二个.m中,我有@synthesis sameDataDictionary;-(void)viewDidLoad{[super viewDidLoad];//更新UI NSLog(@“JSONDATA>%@”,self.sameDataDictionary);}
非常抱歉,感谢您的帮助@Nikos M
- (void)viewDidLoad
{
[super viewDidLoad];
// Set Title
self.title = @"Authors";
dispatch_async(dispatch_get_main_queue(), ^{
// Update the UI
NSURL *blogURL = [NSURL URLWithString:@"http://www.how-e.co.uk/test.json"];
NSLog(@"BLOG URL > %@", blogURL);
NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];
NSLog(@"JSONDATA > %@", jsonData);
NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
//Here pass the data to the NSDictionary of the secondViewController
secondViewController.sameDataDictionary = [[NSMutableDictionary alloc]initWithDictionary:dataDictionary];
NSLog(@"JSONDATA > %@", jsonData);
self.authors = [dataDictionary objectForKey:@"Root"];
self.searchResults = [NSMutableArray arrayWithCapacity:[authors count]];
[self.tableView reloadData];
});