2011-03-11 43 views
2

我目前有一个桌面视图嵌入在自定义单元格内的YouTube视频。在tableview单元格中的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]; 

} 

回答

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 
     [email protected]"Loaded"; 
    } 
    return cell; 
} 

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

dictionary = [[NSMutableDictionary alloc] init]; 

头文件

NSMutableDictionary *dictionary; 

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