我目前有自定义单元格内嵌有youtube视频的tableview。
我之所以这样做是因为从我的研究中看来,这似乎是允许视频加载而不离开我的应用程序的唯一方法。
问题在于:
缩略图需要一段时间才能加载。
当我向下滚动视频列表时,它不断加载thunbnails。
如果我向上滚动...它会再次尝试加载视频缩略图。
有没有人对更好的方法有什么建议,或者让表格单元格保留数据而不是替换它的方法?
我的代码如下所示:
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;
干杯
答案 0 :(得分:3)
在这些情况下我做的是创建一个函数,用我需要的表格单元格填充可变数组 然后在方法中:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
我根据索引返回数组中的单元格:
return [cellArray objectAtIndex:indexPath.row];
我用这种方式创建了非常大的可变数组并且没有内存问题,但我想这取决于你应用中的其他内容。
答案 1 :(得分:1)
仅缓存Web视图,而不是整个表格单元格。这样你仍然可以重用细胞。 我从这里使用了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方法中替换单元格的YouTubeView:
YouTubeView * youTubeView = [self.myVideosCache objectAtIndex:indexPath.row];
[cell.contentView addSubview:youTubeView];
请务必从单元格的子视图层次结构中删除旧的YouTubeView 当表滚动时我发现内存泄漏,这可能是在UITableView中使用UIWebView的一般问题。
答案 2 :(得分:0)
不要将表格单元格出列并重新使用。由于您一直在分配新的单元格,因此会有性能受损,但它应该有效。当你收到内存警告然后开始释放单元格。