0

我设法使用 AFNetworking 在 UITable 视图上解析来自 YouTube API 的数据,但我不知道如何使用

didSelectRowAtIndexPath:(NSIndexPath *)indexPath

如何将 JSON 数据传递到下一个视图?

这是文件的链接:google drive

这是我的代码

#import "KKViewController.h"

#import "AFNetworking.h"


#import "AFJSONRequestOperation.h"

@interface KKViewController ()

@end

@implementation KKViewController

@synthesize  movies = _movies, count = _count;


- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.movies = [[NSArray alloc] init];

    NSURL *url = [[NSURL alloc] initWithString:@"http://gdata.youtube.com/feeds/api/playlists/PL0l3xlkh7UnvLdr0Zz3XZZuP2tENy_qaP?v=2&alt=jsonc&max-results=50"];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {



        self.movies = [JSON valueForKeyPath:@"data.items.video"];

        // NSLog(@@, video)

        //


        self.count = [JSON valueForKeyPath:@"data.items.video.thumbnail"];
        [self.activityIndicatorView stopAnimating];
        [self.tableView setHidden:NO];
        [self.tableView reloadData];

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
    }];

    [operation start];





    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

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

    if (self.movies && self.movies.count) {
        return self.movies.count;
    } else {
        return 0;
    }


}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"Cell Identifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
    }

    NSDictionary *movie = [self.movies objectAtIndex:indexPath.row];


    NSDictionary *countt = [self.count objectAtIndex:indexPath.row];
    cell.textLabel.text = [movie objectForKey:@"title"];
    cell.detailTextLabel.text = [movie objectForKey:@"uploader"];

    NSURL *url = [[NSURL alloc] initWithString:[countt objectForKey:@"hqDefault"]];
    [cell.imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder"]];

    return cell;
}
4

2 回答 2

1

您可以:

  1. 在你的故事板原型中为有问题的单元定义一个segue,你根本不需要写一个didSelectRowAtIndexPath

  2. 已经didSelectRowAtIndexPath做了一个performSegueWithIdentifier(给它你在情节提要中为你的segue设置的唯一字符串“情节提要标识符”;

然后,您可以编写一个prepareSegue查看表格indexPathForSelectedRow以确定选择了哪一行的代码。然后,您可以从应用程序的模型中获取适当的信息,并在destinationViewController.

您也可以didSelectRowAtIndexPath设置instantiateViewControllerWithIdentifier属性,然后执行适当的pushViewControlleror presentViewController,但我个人更喜欢利用您在情节提要中定义的 segues 的上述技术。

于 2013-02-05T19:13:25.820 回答
1

我希望这个示例代码能帮助您轻松完成您的需求。请问您是否有任何模棱两可的地方。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath   *)indexPath
 {
  [self.leadsTableView deselectRowAtIndexPath:indexPath animated:YES];
  id object = [[self.tableData objectAtIndex:indexPath.section]    objectAtIndex:indexPath.row];
self.leadforLeadDetails = (Lead*)object;

self.selectedLeadID = self.leadforLeadDetails.ID;
[self performSegueWithIdentifier:@"openLeadDetails" sender:self];


// Navigation logic may go here. Create and push another view controller.
/*
 <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
 // ...
 // Pass the selected object to the new view controller.
 [self.navigationController pushViewController:detailViewController animated:YES];
 */
}


 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
   //NSIndexPath *indexPath = [self.tableView2 indexPathForCell:sender];
   //[self.tableView2 deselectRowAtIndexPath:indexPath animated:YES];
   if ([segue.identifier isEqualToString:@"openLeadDetails"])
{
       LeadsDetailsTableViewController *destViewController = segue.destinationViewController;
       destViewController.dictLeadDetails = self.dictLeadDetails;

       destViewController.leadID = self.leadforLeadDetails.ID;
       destViewController.Name = self.leadforLeadDetails.Name;
       destViewController.leadDetails.ID = self.leadforLeadDetails.ID;
       destViewController.leadDetails.Name = self.leadforLeadDetails.Name;
     }
 }
于 2013-02-05T19:42:08.380 回答