我正在将 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;
}
}
谁能告诉我实现过程中出了什么问题?
最佳答案
我想您的代码在管理单元格 View 出队方面是正确的(例如,当您分配 self.contentView 时,我假设此 contentView 是一个 View Controller 属性,分配为以“LoadYouTubeVid”标识的表单元格 View 的导出)。因此,假设您的代码在进行基本表管理时是正确的,那么其余的似乎都没有问题。
我过去遇到过类似的问题,我发现解决它的方法是摆脱单元格 View 队列。在这种情况下,我以这种方式获取我的单元格,其中每个单元格都有不同的标识符。当然,我需要以编程方式创建单元格。这行得通。
NSString *cellId = [NSString stringWithFormat:@"YouTubeCell_%d",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if(!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
[cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
cell.frame=CGRectMake(0, 0, 320, 242);
UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 29)];
titleLbl.tag=101;
titleLbl.backgroundColor=[UIColor lightGrayColor];
titleLbl.font=[UIFont fontWithName:@"Verdana" size:18.0];
titleLbl.textAlignment=UITextAlignmentCenter;
titleLbl.textColor=[UIColor blackColor];
titleLbl.shadowColor=[UIColor whiteColor];
titleLbl.shadowOffset=CGSizeMake(0, -1);
[cell.contentView addSubview:titleLbl];
[titleLbl release];
YouTubeView *ytView = [[YouTubeView alloc] initWithFrame:CGRectMake(0, 29, 320, 213)];
ytView.tag=102;
[cell.contentView addSubview:ytView];
[ytView release];
PostModel *post = [self.notizie postAtIndex:indexPath.row];
//UILabel *titleLabel = (UILabel *)[cell viewWithTag:101];
titleLbl.text = post.title;
NSString *ytURL = [NSString stringWithFormat:@"http://www.youtube.com/watch?v=%@",[post.video lastPathComponent]];
//NSString *ytURL = post.video;
//YouTubeView *ytView = (YouTubeView *)[cell viewWithTag:102];
[ytView setURL:ytURL];
}
总而言之,这种方法还不错。内存中确实有一些额外的 UITableView 单元格,但考虑到大部分内存都被缓存的 UIWebView 占用了。在这种情况下,您不再需要缓存它,因为它存储在 TableView 中。
关于objective-c - 无法在 UITableViewCells 中正确设置 UIWebview(带有 Youtube 视频),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7695391/