我目前有一个自定义单元格内嵌YouTube视频的表格 View 。
我这样做是因为从我的研究看来,这是允许在不离开我的应用程序的情况下加载视频的唯一方法。
问题是这样的
缩略图需要一段时间才能加载。当我向下滚动视频列表时,它总是不得不加载缩略图。如果我向上滚动,它将尝试再次加载视频缩略图。
有没有人对更好的方法或使表格单元格保留数据而不替换数据的方法有任何建议?
我的代码如下所示:
-(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];
}
最佳答案
您应该缓存加载的图像。
一种方法是创建例如可变字典,在其中存储带有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;
}
在头文件中创建一个名为“dictionary”的
NSMutableDictionary
,并在viewDidLoad
中对其进行初始化:dictionary = [[NSMutableDictionary alloc] init];
头文件:
NSMutableDictionary *dictionary;
这将导致以下行为:
第一次显示您的单元格,它显示为“已加载”。在所有后续外观中,它将显示其设定值。
关于objective-c - 表格 View 单元格中的YouTube视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5270743/