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?
Asked
Active
Viewed 30 times
-1
-
request made only once or request made when the cell is visible to user ? – Teja Nandamuri Feb 11 '16 at 17:52
-
Or you can have a bool flag and set it to NO, after the first time in the cell for rowat index method. – Teja Nandamuri Feb 11 '16 at 17:56
1 Answers
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
- initiate it inside the
viewDidLoad
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