2011-03-11 67 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; 

這將導致以下行爲: 第一次顯示您的單元格時,顯示「加載」。在隨後的所有外觀中,它將顯示其設定值。