2016-07-26 21 views
0

我在UITableView的大部分單元格中都有UIWebView,它們用於顯示視頻。
在大多數情況下,細胞數量會少一些,但這些可能是無止境的。在滾動UITableView; webView每次都會重新載入其數據。爲UItableview中的每一行添加新的(不可重用的)單元格swift

任何人都可以有解決方案如何爲每行創建新單元而不是使用dequeueReusableCellWithIdentifier以防止在UIWebView中重新加載。


我還是不喜歡每次都創建新的單元格。
但是然後如何防止每次重新加載webView?

+0

你能告訴你的cellForRowAtIndexPath,所以我可以很容易地告訴你如何解決您的問題。 – pnizzle

回答

0

您可以創建NSIndexPath字典作爲鍵和UITableViewCell的作爲價值

// MARK: - Properties 

private var cells: [NSIndexPath: VideoCell] = [:] 
private var videos: [Video] = [] 

// MARK: - UITableViewDataSource 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    if let cachedCell = self.cells[indexPath] { 
     return cachedCell 
    } 
    else { 
      let videoCell = tableView.dequeueReusableCellWithIdentifier(VideoCell.ReuseIdentifier, forIndexPath: indexPath) as! VideoCell 
      videoCell.bindWith(self.videos[indexPath]) 
      self.cells[indexPath] = videoCell 
      return videoCell 
     } 
    } 

// MARK: - UITableViewDelegate 

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 
    guard let videoCell = cell as? VideoCell else { 
     return 
    } 
    videoCell.refresh() 
} 

如果你將有很多細胞的可能與內存問題朝上。

0

如果您考慮效率和性能,只需重新加載從未播放過的視頻,並且同時將視頻數據或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) 
} 
+1

@EricD謝謝,更新了答案。 – Yahoho

相關問題