0

我有一个表格视图,当您按下一个单元格时,它会将您带到另一个表格视图。它使用相同的 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];

}

第二张桌子

-(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
4

2 回答 2

0

为什么要在第二个表中再次解析字典。创建一个自定义初始化程序,例如

第二页.h

  @property (nonatomic ,retain) NSMutableArray *searchResultarray;
 -(id)initWithSearchResults:(NSMutableArray*)searchResult;

并使用收到的数组作为表的数据源

第二页.m

@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];
 }
于 2013-10-27T10:26:09.490 回答
0

在第二个视图控制器中声明一个 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];

    });
于 2013-10-27T12:09:11.787 回答