セルを押すと別のテーブルビューに移動するテーブルビューがあります。同じjsonフィードを使用し、フィードを両方のテーブルのviewdidloadメソッドに入れています。を押して次のテーブルビューに移動すると、読み込みが遅くなります。フィードが再度読み込まれるためだと思います。json フィードを最初のテーブルに配置してから次のテーブルに運ぶことはできますか? その場合、再度ロードする必要はありません。
誰かがあなたのタイムコードに感謝します
ビューがロードされました
- (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];
}
2番目のテーブル
-(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"];
}
}
}
#pragma mark -
#pragma mark Initialization
- (id)initWithStyle:(UITableViewStyle)style {
self = [super initWithStyle:style];
if (self) {
}
return self;
}
#pragma mark -
#pragma mark View Life Cycle
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Books";
[self connect];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark -
#pragma mark Getters and Setters
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.books count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"HistoryCell";
// Similar to UITableViewCell, but
InnerCell *cell = (InnerCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[InnerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
NSDictionary *book = [self.books objectAtIndex:[indexPath row]];
[cell.descriptionLabel setText:[book objectForKey:@"Title"]];
[cell.tlabel setText:[book objectForKey:@"Title"]];
cell.ticon.image = [UIImage imageNamed:[book objectForKey:@"Cover"]];
return cell;
}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [indexPath row] + 100;
}
#pragma mark -
#pragma mark Table View Delegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Initialize Book Cover View Controller
MTBookCoverViewController *bookCoverViewController = [[MTBookCoverViewController alloc] initWithNibName:@"MTBookCoverViewController" bundle:[NSBundle mainBundle]];
// Fetch and Set Book Cover
NSDictionary *book = [self.books objectAtIndex:[indexPath row]];
UIImage *bookCover = [UIImage imageNamed:[book objectForKey:@"Cover"]];
[bookCoverViewController setBookCover:bookCover];
UIImage *bookCover2 = [UIImage imageNamed:[book objectForKey:@"Cover"]];
[bookCoverViewController setBookCover2:bookCover2];
[bookCoverViewController setBookTag:[book objectForKey:@"Title"]];
// Push View Controller onto Navigation Stack
[self.navigationController pushViewController:bookCoverViewController animated:YES];
}
@end