【问题标题】:UIWebview within UITableViewCellUITableViewCell 中的 UIWebview
【发布时间】:2016-02-11 17:49:40
【问题描述】:

我有一个包含 Web 视图的自定义单元格 - 问题是我只想为 Web 视图调用一次加载 URL 的请求。现在我在 cellForRowAtIndex: 中加载请求,当然这会多次调用请求。我如何确保请求只发出一次,最好的方法是什么?

【问题讨论】:

  • 请求只发出一次或在单元格对用户可见时发出请求?
  • 或者你可以有一个 bool 标志并将其设置为 NO,在第一次在单元格中用于 rowat 索引方法之后。

标签: ios objective-c uitableview uiwebview


【解决方案1】:

维护一个数据结构来存储来自网络请求的响应将解决这种情况。比如说indexpath 第一次在数据结构中查找(NSMutableArray 会更合适),当您第一次查找数据时,您不会找到任何数据,然后获取响应并将其存储到数据结构。然后第二次当您在数据结构中查找同一indexpath 的单元格时,瞧,这一次您不必提出请求。

为简单起见,我使用 NSMutableDictionary 命名为 responseData

  1. viewDidLoad 内启动它
  2. cellForRowAtIndexPath

    UITableViewCell *cell = //deque the cell
    
    NSString *responseKey = [NSString stringWithFormat:"%d,%d", indexPath.section,indexPath.row];
    
    NSString *htmlData = responseData[responseKey];
    
    if(htmlData == nil)
    {
        htmlData = //whatever the way do your request, put the response in the htmlData. as you are doing asynchronous loading of the web-request, set the cell's webView in the completion. NB:: do gui related work on gui/main thread.
    
        responseData[responseKey] = htmlData;
    }
    
    //set the htmlData to your, cell's webView
    

【讨论】:

  • 对于html内容的帮助&UIWebView可以看一下this
猜你喜欢
  • 2013-11-07
  • 1970-01-01
  • 1970-01-01
  • 2011-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-09
  • 1970-01-01
相关资源
最近更新 更多