在循环之外的objectatindex?

时间:2015-10-21 06:35:54

标签: objective-c uitableview nsinteger

这是一个很难问的问题,但无论如何我都会试一试。我正在尝试检索UITableCell循环之外的NSDictionary的内容。现在,当我通过以下方式检索其内容时:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
一切正常。但是在该循环之外,在它自己的void函数中,检索的数字是0.

- (void)getVideoList{



NSString *ensdsds = @"z9yDgV3ONSU";



for (int i=0; i< [self.youtubePaginator.results count]; i++){

    NSDictionary *photoshoots = [self.youtubePaginator.results objectAtIndex:i];
    NSString * videoId = photoshoots[@"videoID"];

    NSLog(@"Newly %@: ", videoId);



    NSString *urlStrings = [NSString stringWithFormat:@"https://www.googleapis.com/youtube/v3/videos?part=id%%2C+snippet%%2C+contentDetails%%2C+statistics&id=%@&key=78587868", videoId];

    NSLog(@"Arries %@", urlStrings);

    NSURL *urlstats = [NSURL URLWithString:urlStrings];
    NSURLRequest *requeststats = [NSURLRequest requestWithURL:urlstats];
    AFHTTPRequestOperation *operationtwo = [[AFHTTPRequestOperation alloc] initWithRequest:requeststats];
    operationtwo.responseSerializer = [AFJSONResponseSerializer serializer];
    [operationtwo setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operationtwo, id responseObjected)
     {
         NSDictionary *itemed = [responseObjected objectForKey:@"items"];
         for (NSDictionary *itemest in itemed )
         {
             YouTubeVideo *youTubeVideo = [[YouTubeVideo alloc] init];
             NSDictionary* stats = [itemest objectForKey:@"statistics"];
             youTubeVideo.likesCount = [stats objectForKey:@"likeCount"];
             youTubeVideo.viewsCount = [stats objectForKey:@"viewCount"];

             NSDictionary* channelInfo = [itemest objectForKey:@"snippet"];
             youTubeVideo.channelInfo = [channelInfo objectForKey:@"channelId"];
             youTubeVideo.videoUploader = [channelInfo objectForKey:@"channelTitle"];


             NSLog(@"True To: %@", youTubeVideo.videoUploader);
             [self.thunder addObject:youTubeVideo];


         }} failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {

         NSLog(@"Error loading %@", error);
     }];

    [operationtwo start];    }

}

以上代码不返回任何内容。如何在void函数中合并NSIndexPath,并且仍然可以在[self getVideoList];中调用viewDidLoad

希望有道理吗? :/

1 个答案:

答案 0 :(得分:1)

您可以通过imageURL作为参数获取getVideoList中的NSIndexPath,如果您想全局访问它,则只需创建一个全局NSString对象或您可以将提取的imageURL作为返回值返回。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        NSString * imageURL = [self getVideoList:indexPath];

        // Write your code to perform operation with the image URL.
}

以下是getVideoList方法

- (NSString *)getVideoList:(NSIndexPath *)indexPath{    

    NSDictionary *photoshoots = [self.youtubePaginator.results objectAtIndex:indexPath.Row];
    NSString * imageURL = photoshoots[@"avatar_url"];
    NSLog(@"Newly %@: ", imageURL);

    NSLog(@"To find us: [%d]:%@",i,self.youtubePaginator.results[i]);

    return imageURL;
}

您可以根据自己的要求更新代码。