【发布时间】:2011-10-08 07:23:04
【问题描述】:
我将 UIWebView 添加到我的表格单元格中,然后将 youtube 视频 HTML 嵌入其中。我遇到的这个问题是,在我再次向下和向上滚动表格后(重复使用单元格时),视频无法正确显示。视频在错误的单元格中重复和混乱。我的实现如下。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* PlaceholderCellIdentifier = @"LoadYoutubeVid";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:PlaceholderCellIdentifier];
if (cell == nil)
{
//Add youtube video to the cell contentview
UIWebView *youtubeVideo = [[UIWebView alloc]init];
youtubeVideo.tag = YOUTUBE_WEBVIEW;
[self.contentView addSubview:youtubeVideo];
[youtubeVideo release];
}
//Set youtube video (if exist)
//Retrieve youtube webview from cell's contentview
UIWebView *thisYouTubeVideo = (UIWebView *)[self.contentView viewWithTag:YOUTUBE_WEBVIEW];
//Only set if the cell is suppose to have a youtube webview
if (self.answerForCell.youtube_url !=nil)
{
//Set the frame of the youtube webview
thisYouTubeVideo.frame = CGRectMake(CELL_TEXT_LEFT_MARGIN + CELL_AVATAR_WIDTH + CELL_SPACING, currentYAxisValue, CELL_YOUTUBEVIEW_WIDTH, CELL_YOUTUBEVIEW_HEIGHT);
//Set the key for the youtube webview dictionary (acts as cache)
NSString *videokey=[NSString stringWithFormat:@"%@",self.answerForCell.youtube_url];
//Check if the video key exist in cache
if([self.youtubeVideoCache objectForKey:videokey]) {
//Replace the youtube webview with the one in the cache *THINK THIS IS WHERE THE ISSUE IS... BUT NOT SURE WHAT
thisYouTubeVideo = [self.youtubeVideoCache objectForKey:videokey];
}
else //If first time loading cell and webview
{
//Create a new videoHTML to be embedded in the youtube webview
NSString * videoHTML = [self embedYouTube:self.answerForCell.youtube_url frame:CGRectMake(CELL_TEXT_LEFT_MARGIN + CELL_AVATAR_WIDTH + CELL_SPACING, currentYAxisValue, CELL_YOUTUBEVIEW_WIDTH, CELL_YOUTUBEVIEW_HEIGHT)];
//Embed the new video HTML to the youtube webview
[thisYouTubeVideo loadHTMLString:videoHTML baseURL:nil];
//But I want to create a new webview (different address) before storing it into the dictionary cache
UIWebView *newYouTubeVideo = [[UIWebView alloc]initWithFrame:CGRectMake(CELL_TEXT_LEFT_MARGIN, currentYAxisValue, CELL_YOUTUBEVIEW_WIDTH, CELL_YOUTUBEVIEW_HEIGHT)];
//Load the videoHTML to the newly created youtube webview before storing it into cache
[newYouTubeVideo loadHTMLString:videoHTML baseURL:nil];
//Store the new webview into cache
[self.youtubeVideoCache setObject:newYouTubeVideo forKey:videokey]; //Save webview in dictionary
}
}
else
{
//If cell does not have a youtube webview, set the frame to zero
thisYouTubeVideo.frame = CGRectZero;
}
}
谁能告诉我我的实现出了什么问题?
【问题讨论】:
-
您找到解决方案了吗?
标签: objective-c ios uitableview uiwebview