如果您考慮效率和性能,只需重新加載從未播放過的視頻,並且同時將視頻數據或html數據緩存到本地,則必須重用該單元並重新加載單元格中的web視圖。另一方面,對於播放單元格,您可以記錄webview的對象,直接將這些webviews設置爲重用單元格。
僞代碼:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
YourCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier" forIndexPath:indexPath];
// If the video at this row is playing, just setup webview
UIWebView *webView = [self webviewOfPalyingForIndexPath: indexPath];
if (webView) {
[cell setupWithWebview: webView];
} else {
// Configure cell with reused webview
static UIWebView *publicWebview = [UIWebView new];
[cell setupWithWebview: publicWebview];
// Check if the data of webview is cached
NSData *cachedData = [self readCachedDataForIndexPath: indexPath];
if (cachedData) {
[cell setupWithCachedData:cachedData];
} else { // If not cached, load url and cach the data
[cell setupWithUrl: your_url];
[self cacheDataForUrl: your_url];
}
}
return cell;
}
// Record webview of playing at indexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
YourCell *cell = (YourCell *)[tableView cellForRowAtIndexPath:indexPath];
[self recordPlayingWebView: cell.webview atIndexPath: indexPath];
}
希望我的回答對你有用。
UPDATE:
與僞銀行代碼:
let reusedWebview = UIWebView()
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CellIdentifier", forIndexPath: indexPath)
// If the video at this row is playing, just setup webview
let webView = self.webviewOfPalyingForIndexPath(indexPath)
if (webView != nil) {
cell.setupWithWebview(webView)
} else {
// Configure cell with reused webview
cell.setupWithWebview(resuedWebview)
// Check if the data of webview is cached
let cachedData = self.readCachedDataForIndexPath(indexPath)
if (cachedData != nil) {
cell.setupWithCachedData(cachedData)
} else { // If not cached, load url and cach the data
cell.setupWithUrl(your_url)
self.cacheDataForUrl(your_url)
}
}
return cell;
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)
self.recordPlayingWebView(cell.webview, forIndexPath: indexpath)
}
你能告訴你的cellForRowAtIndexPath,所以我可以很容易地告訴你如何解決您的問題。 – pnizzle