2016-07-26 3 views
0

UITableView의 셀 대부분에 UIWebView이 있는데,이 셀은 비디오를 표시하는 데 사용됩니다.
대부분의 경우 셀 수가 적지 만 끝이 없을 수 있습니다. 스크롤 할 때 UITableView; webView는 매번 데이터를 다시로드합니다.UItableview swift에서 모든 행에 새 (재사용 할 수없는) 셀 추가

누구나 dequeueReusableCellWithIdentifier 대신 UIWebView에서 다시로드하지 않고 모든 행에 대해 새 셀을 만드는 방법을 사용할 수 있습니까?


나는 항상 새 셀을 만드는 것을 선호하지 않습니다.
그렇다면 매번 webView를 다시로드하지 못하도록하는 방법은 무엇입니까? 당신은 당신이 메모리 문제와 직면 할 수있는 셀이 많은 경우

+0

당신이 당신의 cellForRowAtIndexPath을 표시 할 수 있습니다. – pnizzle

답변

0

당신은 값

// 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() 
} 

으로 키와있는 UITableViewCell로 NSIndexPath과 사전을 만들 수 있습니다.

0

효율성과 성능을 고려해 본 적이없는 셀을 다시로드하고 동시에 비디오 데이터 또는 HTML 데이터를 로컬에 캐시하면 셀을 재사용하고 셀에서 웹뷰를 다시로드해야합니다 . 반면에, 셀을 재생하려면 웹뷰의 객체를 기록하고,이 웹뷰를 재사용 된 셀에 직접 설정할 수 있습니다.

의사 코드 :

- (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

관련 문제