-1

I have a custom cell that contains a web view - the thing is I would like to only call the request to load a URL once for the web view. Right now I loading the request within cellForRowAtIndex: and of course this is making the request called many times. How do I make sure the request is made only once and what is the most optimal way to do this?

Mike Simz
  • 3,976
  • 4
  • 26
  • 43

1 Answers1

0

Maintaining a data-structure to store the responses from the web-request will solve this scenario. say for indexpath first time you look up in the data structure (NSMutableArray would be more appropriately), as you are looking for the data for the first time, you wont find any, then fetch the response & store it into the data-structure. Then second time when the cell at the same indexpath you lookup in the data-structure, voila this time you don't have to make the request.

For simplicity i'm using NSMutableDictionary named responseData

  1. initiate it inside the viewDidLoad
  2. in the 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
    
Ratul Sharker
  • 7,484
  • 4
  • 35
  • 44
  • for help in html content & `UIWebView` can have a look in [this](http://stackoverflow.com/questions/992348/reading-html-content-from-a-uiwebview) – Ratul Sharker Feb 11 '16 at 18:38