2016-08-16 61 views
0

更新1中:
這裏是cellForItemAtIndexPathYTPlayerView一個UICollectionView動態小區

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
    MBDynamicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath: 
            indexPath]; 
    video= videoData[indexPath.row]; 

    if(!cell) { 
     cell = [MBDynamicCollectionViewCell new]; 
    } 

    [cell.videoPlayer loadWithVideoId:video.videoID]; 

    cell.label1.text = [NSString stringWithFormat:@"%@",video.videoTitle]; 

    return cell; 
} 

代碼,這裏是我如何發送請求到服務器

NSURL *url=[ NSURL URLWithString:@"https://www.googleapis.com/youtube/v3/search?key=AIzaSyClU73k2yYl7gufFDxHU7uhGSCuM78f7Aw&channelId=UCW9CKYMhpJM-B1B0PGBB9Ew&part=snippet,id&order=date&maxResults=20"]; 

NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
NSOperationQueue *queue= [[NSOperationQueue alloc] init]; 
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response,NSData *data,NSError *error){ 
    if(error){ 
     NSLog(@"error:%@", error.localizedDescription); 
    } 
    else{ 



     NSError *error; 
     NSMutableDictionary *allVideos= [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error]; 
     // NSString *string=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
     // NSLog(@"%@",string); 
     for(NSDictionary *dictionary in allVideos[@"items"]){ 

      NSDictionary *videoID=dictionary[@"id"]; 
      // NSLog(@"video id is %@", videoID[@"videoId"]); 
      NSDictionary *snippet= dictionary[@"snippet"]; 
      //NSLog(@"video title is %@", snippet[@"title"]); 
      NSDictionary *thumbnails= dictionary[@"snippet"][@"thumbnails"][@"default"]; 
      //NSLog(@"video image url is %@",thumbnails[@"url"]); 

      video=[[MBVideoClass alloc] initWithID:videoID[@"videoId"] withTitle:snippet[@"title"] withImg:thumbnails[@"url"] delegate:self]; 

      [videoData addObject:video]; 
     } 
     videoData= [NSMutableArray arrayWithArray:[videoData filteredArrayUsingPredicate:pred]]; 
     [self.collectionView reloadData]; 
    } 

}]; 

一部開拓創新的問題

我有一個dded YTPlayerView裏面有一個UICollectionView動態單元格。
我的應用程序在創建新的Web視圖時出現「訪問錯誤」時發生錯誤
當我嘗試在collectionView:cellForItemAtIndexPath:內部初始化webView時出現此問題。
有什麼想法?

1 0x7133239 StartWebThread() 
2 0x37579b7 __pthread_once_handler 
3 0x37695eb _os_once 
4 0x3757966 pthread_once 
5 0x7132fce WebThreadEnable 
6 0x610181a +[WebView(WebPrivate) enableWebThread] 
7 0x60fbcb0 WebKitInitialize 
8 0x85b8f0 ___UIApplicationLoadWebKit_block_invoke 
9 0x34109cd _dispatch_client_callout 
10 0x33fa26d dispatch_once_f 
11 0x33fa1cb dispatch_once 
12 0x85b7f8 _UIApplicationLoadWebKit 
13 0x21d9f59 _class_initialize 
14 0x21df981 lookUpImpOrForward 
15 0x21df8e1 _class_lookupMethodAndLoadCache3 
16 0x21ea547 objc_msgSend 
17 0x33161 -[MBVideoListCollectionViewController collectionView:cellForItemAtIndexPath:] 
18 0x11e45ee -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:] 
19 0x11e42ff -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] 
20 0x11e8761 -[UICollectionView _updateVisibleCellsNow:] 
21 0x11ed7df -[UICollectionView layoutSubviews] 
22 0x92b3d4 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] 
23 0x21ed059 -[NSObject performSelector:withObject:] 
24 0x5c5b096 -[CALayer layoutSublayers] 
25 0x5c4e8b6 CA::Layer::layout_if_needed(CA::Transaction*) 
26 0x5c4e71a CA::Layer::layout_and_display_if_needed(CA::Transaction*) 
27 0x5c40ee7 CA::Context::commit_transaction(CA::Transaction*) 
28 0x5c75847 CA::Transaction::commit() 
29 0x5c75bef CA::Transaction::release_thread(void*) 
30 0x375929d _pthread_tsd_cleanup 
31 0x3758e22 _pthread_exit 
+1

你能發表一些代碼嗎? –

回答

1

在更新問題時,我意識到我做錯了什麼。
問題是我從後臺線程重新加載collectionView。
因此以下行固定崩潰

dispatch_async(dispatch_get_main_queue(), ^{ 
    [self.collectionView reloadData]; 
}); 

希望這有助於任何人。

相關問題