【问题标题】:Youtube Videos in tableview cells表格视图单元格中的 Youtube 视频
【发布时间】:2011-07-13 08:19:57
【问题描述】:

我目前有一个表格视图,其中嵌入了自定义单元格中的 YouTube 视频。

我这样做是因为根据我的研究,这似乎是允许视频在不离开我的应用程序的情况下加载的唯一方法。

问题是这样的

缩略图需要一段时间才能加载。当我向下滚动视频列表时,它必须不断加载缩略图。如果我向上滚动,它会再次尝试加载视频缩略图。

有没有人对更好的方法或让表格单元格保留数据而不是替换它的方法有任何建议?

我的代码如下所示:

-(UITableViewCell *)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

static NSString *MyIdentifier = @"MyIdentifier";

YouTubeCell *cell = (YouTubeCell*)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if(cell ==nil){

    cell = [[[YouTubeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];

}
NSDictionary *dic = [youTubeArr objectAtIndex:indexPath.row];
[cell updateCellData:dic];
return cell;

}

-(void)updateCellData:(NSDictionary*)dict
{
NSDictionary *tempDic = [dict objectForKey:@"video"];
self.titleLbl.text = [tempDic objectForKey:@"title"];
NSString *viewCountStr = [NSString stringWithFormat:@"%@ views -",[tempDic objectForKey:@"viewCount"]];
viewCountLbl.text = viewCountStr;
uploadedDateLbl.text = [tempDic objectForKey:@"uploaded"];

NSDictionary *videoDic = [tempDic objectForKey:@"player"];
NSString *videoStr = [NSString stringWithFormat:@"%@",[videoDic objectForKey:@"default"]]; 

NSString *embedHTML = 
@"<html><head>\
<body style=\"margin:0\">\
<embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";

// videoView = [[UIWebView alloc] initWithFrame:CGRectMake(3, 5, 100, 60)]; initialzed in ///initwithstyle of cell

NSString *html = [NSString stringWithFormat:embedHTML, videoStr, videoView.frame.size.width, videoView.frame.size.height];
[videoView loadHTMLString:html baseURL:nil];

}

【问题讨论】:

    标签: objective-c uitableview youtube


    【解决方案1】:

    你应该缓存你加载的图片。

    一种方法可以是创建一个可变字典,例如,您可以在其中存储您的图像,并使用您的UITableViewCells 独有的键。在cellForRowAtIndexPath 中,您可以通过调用例如[dictionary objectForKey:uniquecellidentifier] 来检索相应的图像。如果它返回nil,你知道图像还没有被加载,你应该创建一个请求来这样做。加载完成后,您将图像存储在字典中([dictionary setObject:image forKey:uniquecellidentifier]

    这应该会让你有一个更具体的想法:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }
    
        NSString *cellid=[NSString stringWithFormat:@"Cell%i%i", indexPath.section, indexPath.row];
        if([dictionary objectForKey:cellid]){
            NSLog(@"Retrieving value for %@: %@", cellid, [dictionary objectForKey:cellid]);
            cell.textLabel.text=[dictionary objectForKey:cellid];
            //Show image from dictionary
        }else{
            NSLog(@"Now value set for %@.", cellid);
            [dictionary setObject:[NSString stringWithFormat:@"Testvalue%@", cellid] forKey:cellid]; //Save image in dictionary
            cell.textLabel.text=@"Loaded";
        }
        return cell;
    }
    

    在你的头文件中创建一个NSMutableDictionary命名为“字典”并在viewDidLoad中初始化它:

    dictionary = [[NSMutableDictionary alloc] init];
    

    头文件:

    NSMutableDictionary *dictionary;
    

    这将导致以下行为: 第一次显示您的单元格,它显示“已加载”。在随后的所有外观中,它将显示其设定值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多