UITableView iPhone App中的YouTubevideo
我目前有一个桌面视图embedded在自定义单元格内的YouTubevideo。
我这样做是因为从我的研究中,似乎是让video无需离开我的应用程序就可以加载的唯一方法。
问题是这样的:
缩略图需要一段时间才能加载。
当我向下滚动video列表时,它不断加载thunbnails。
如果我向后滚动,它会再次尝试加载video缩略图。
有没有人有任何build议,要么做更好的方法,或者让表格单元格保持数据,而不是取代它的方法?
我的代码如下所示:
CustomVideoCell *cell = (CustomVideoCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomVideoCell"]; if (cell == nil) { UIViewController *temporaryController = [[UIViewController alloc] initWithNibName:@"CustomVideoCell" bundle:nil]; cell = (CustomVideoCell *)temporaryController.view; [temporaryController release]; GDataEntryBase *entry = [[self.feed entries] objectAtIndex:indexPath.row]; NSString *title = [[entry title] stringValue]; NSString *videoID = [[(GDataEntryYouTubeVideo *)entry mediaGroup] videoID]; NSString *htmlString = [ [NSString alloc] initWithFormat:@"<html><head><meta name = \"viewport\" content = \"initial-scale = 2.0, user-scalable = no, width = 110\"/></head><body style=\"background:#000;margin-top:0px;margin-left:0px\"><div><object width=\"110\" height=\"90\"><param name=\"movie\" value=\"http://www.youtube.com/v/%@&f=gdata_videos&c=ytapi-my-clientID&d=nGF83uyVrg8eD4rfEkk22mDOl3qUImVMV6ramM\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"http://www.youtube.com/v/%@&f=gdata_videos&c=ytapi-my-clientID&d=nGF83uyVrg8eD4rfEkk22mDOl3qUImVMV6ramM\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"110\" height=\"90\"></embed></object></div></body></html>", videoID, videoID ]; [[[cell.web subviews] lastObject] setScrollEnabled:NO]; [cell.web loadHTMLString:htmlString baseURL:[NSURL URLWithString:@"http://www.website.com"]]; cell.title.text = title;
干杯
我在这些情况下做的是创build一个函数来填充我需要的表格单元格的可变数组。
然后在方法中:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
我根据索引返回数组中的单元格,如下所示:
return [cellArray objectAtIndex:indexPath.row];
我已经创build了非常大的可变数组,并没有内存问题,但我想这取决于你的应用程序中的其他东西。
只cachingweb视图,而不是整个表格单元格。 这样你仍然可以重用这些单元格。 我从这里使用YouTubeView http://iosdevelopertips.com/video/display-youtube-videos-without-exiting-your-application.html
-(void) cacheYouTubeViews { NSArray * videos = [self.fetchedResultsController fetchedObjects]; self.myVideosCache = [[NSMutableArray alloc] initWithCapacity:[videos count]]; for (Video * video in videos) { YouTubeView * youTubeView = [[YouTubeView alloc] initWithStringAsURL: video.link frame:CGRectMake(0, 0, kVideoThumbnailSize, kVideoThumbnailSize)]; [self.myVideosCache addObject:youTubeView]; } }
然后在你的cellForRowAtIndexPath方法中replace单元格的YouTubeView:
YouTubeView * youTubeView = [self.myVideosCache objectAtIndex:indexPath.row]; [cell.contentView addSubview:youTubeView];
确保从单元格的子视图层次结构中删除旧的YouTubeView。
当表滚动时,我发现内存泄漏,可能是在UITableView中使用UIWebView的一般问题。
不要出列和重用表格单元格。 性能会受到影响,因为您一直在分配新的单元,但是它应该可以工作。 当你得到一个内存警告,然后开始释放单元格。