Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c UITableViewCell中嵌入的YouTube视频:如何缓存以防止重新加载?_Objective C_Ios_Uitableview_Youtube - Fatal编程技术网

Objective c UITableViewCell中嵌入的YouTube视频:如何缓存以防止重新加载?

Objective c UITableViewCell中嵌入的YouTube视频:如何缓存以防止重新加载?,objective-c,ios,uitableview,youtube,Objective C,Ios,Uitableview,Youtube,我通过以下代码将youtube视频缩略图嵌入我的UITableView单元。但是,每当我滚动表格时,当单元格离开屏幕并返回时,视频缩略图将重新加载 缓存缩略图的最有效方法是什么,以便它们只在第一次加载 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString* PlaceholderCellIdentifi

我通过以下代码将youtube视频缩略图嵌入我的UITableView单元。但是,每当我滚动表格时,当单元格离开屏幕并返回时,视频缩略图将重新加载

缓存缩略图的最有效方法是什么,以便它们只在第一次加载

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* PlaceholderCellIdentifier = @"PlaceholderCell";

    SearchResult *searchResult = [self.searchResultArray objectAtIndex:indexPath.row];

    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:PlaceholderCellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:PlaceholderCellIdentifier]autorelease];
        UIWebView *videoView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 104, 104)];
        videoView.tag = 1;
        cell.imageView.image = [UIImage imageNamed:@"Placeholder"];

        [cell.contentView addSubview:videoView];
        [videoView release];
    }

    UIWebView *thisVideoView = (UIWebView *)[cell.contentView viewWithTag:1];
    NSString *videoHTML = [self embedYouTube:searchResult.url frame:CGRectMake(0, 0, 104, 104)];
    [thisVideoView loadHTMLString:videoHTML baseURL:nil];

    return cell;
}


- (NSString*)embedYouTube:(NSString*)url frame:(CGRect)frame {  
    NSString* embedHTML = @"\
    <html><head>\
    <style type=\"text/css\">\
    body {\
    background-color: transparent;\
    color: white;\
    }\
    </style>\
    </head><body style=\"margin:0\">\
    <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
    width=\"%0.0f\" height=\"%0.0f\"></embed>\
    </body></html>"; 
    NSString* html = [NSString stringWithFormat:embedHTML, url, frame.size.width, frame.size.height];  
    return html;
}  
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*占位符单元格标识符=@“占位符单元格”;
SearchResult*SearchResult=[self.searchResultArray objectAtIndex:indexPath.row];
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:Placeholder CellIdentifier];
如果(单元格==nil)
{
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle重用标识符:占位符CellIdentifier]自动释放];
UIWebView*videoView=[[UIWebView alloc]initWithFrame:CGRectMake(0,0,104,104)];
videoView.tag=1;
cell.imageView.image=[UIImage ImageName:@“占位符”];
[cell.contentView addSubview:videoView];
[视频视图发布];
}
UIWebView*此视频视图=(UIWebView*)[cell.contentView视图带标记:1];
NSString*videoHTML=[self-embeddeyoutube:searchResult.url-frame:CGRectMake(0,0,104,104)];
[thisVideoView加载HTMLString:videoHTML基URL:nil];
返回单元;
}
-(NSString*)(NSString*)url帧:(CGRect)帧{
NSString*embedHTML=@”\
\
\
身体{\
背景色:透明\
颜色:白色\
}\
\
\
\
"; 
NSString*html=[NSString stringWithFormat:embedHTML,url,frame.size.width,frame.size.height];
返回html;
}  

在这种情况下,您可以(在首次分配webView后)将webView与searchResultArray中的对象与objc_SetAssociated对象相关联

问题是,由于每次单元格可见时都会调用CellForOwatineXpath,因此您不应该在此处初始化webview,但在初始化webview时,您可以:

objc_setAssociatedObject([searchResultArray valueForKey:someIndex], &someKey, webView, OBJC_ASSOCIATION_RETAIN_NONATOMIC)
然后在cellForRowAtIndexPath中执行以下操作:

if (objc_getAssociatedObject([searchResultArray valueForKey:indexPath.row, &someKey) != nil) {
    UIWebView *thisVideoView = (UIWebView *)[cell.contentView viewWithTag:1];
    *thisVideoView = objc_getAssociatedObject([searchResultArray valueForKey:indexPath.row, &someKey);
} else {
    ////empty webView or some loading indicator
}
您可以随时初始化Web视图。
&someKey是静态字符,必须是类的全局键。

您找到问题的答案了吗,或者找到了解决方法了吗