【问题标题】:Unable to set UIWebview (with Youtube Videos) correctly in UITableViewCells无法在 UITableViewCells 中正确设置 UIWebview(带有 Youtube 视频)
【发布时间】: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


【解决方案1】:

我想您的代码在管理单元格视图出队时是正确的(例如,当您分配 self.contentView 时,我假设此 contentView 是一个视图控制器属性,分配给以“LoadYouTubeVid”标识的表格单元格视图的出口)。因此,假设您的代码在进行基本表管理时是正确的,那么其余的似乎都可以。

我过去遇到过类似的问题,我发现解决它的方法是摆脱单元格视图队列。在这种情况下,我以这种方式获取我的单元格,其中每个单元格都有不同的标识符。当然,我需要以编程方式创建单元格。这行得通。


    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 占用。在这种情况下,您不再需要缓存它,因为它存储在表视图中。

【讨论】:

  • 感谢您的建议。但是,我很难这样做,因为我已经在单元格中实现了许多其他东西,即多个标签、uiimageview 等。在这种情况下,改变 UITableviewCell 的整个工作方式太乏味了。对不起,我没有把我的案子说得很清楚。不过还是谢谢你的建议:)
猜你喜欢
  • 2015-05-05
  • 2017-02-10
  • 2014-02-18
  • 1970-01-01
  • 2011-11-24
  • 2011-11-23
  • 1970-01-01
  • 2021-01-01
  • 1970-01-01
相关资源
最近更新 更多