使用相同json feed的两个tablesviews

时间:2013-10-27 10:07:55

标签: ios iphone json uitableview

我有一个表视图,当你按下一个单元格时,它会带你到另一个表视图。它使用相同的json feed并将feed放在两个表的viewdidload方法中。当你按下去下一个表格查看它加载缓慢,我猜测,因为它再次加载饲料。我可以把json feed放在第一个表中,然后将它带到下一个表中,这样它就不需要再加载它了,如果是这样的话我该怎么做呢

如果有人可以帮助,那么感谢您的时间代码在下面

视图确实已加载

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

2 个答案:

答案 0 :(得分:0)

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

secondpage.h

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

并使用收到的数组作为

中表格的数据源

secondpage.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];
 }

答案 1 :(得分: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];

    });