Question

This is the current logic that I'm using to populate a table view with NSURLConnection. It doesn't seem elegant to me.

Table View Controller's viewDidLoad method calls "sendConnection" method in my api wrapper class with the URL string as a parameter. This method makes the NSURLConnection. In connectionDidFinishLoading (which is in my wrapper class), another method is called (also in the wrapper class) with the connection as a parameter. This method extracts the URL from the connection object and examines it. It then uses a switch statement to deal with the data depending on the URL. The data is stored in variables in the wrapper class itself. By the time cellForRowAtIndexPath is called, the async call has finished and the data has been processed.

Is there a better way of doing this?

My reason for asking this is as follows: I want to refresh a cell with a new height and a new text label when it is clicked. The data for this text label will be retrieved from the server upon the cell being tapped. Each cell will have slightly different data in the label (each cell represents a 'user' and the label will display how many mutual friends you have with the user). I want to store the data in the cell itself when the data is retrieved and then place it into the text label. This doesn't seem possible with my current way of making URL calls.

Any help with how to achieve this would be appreciated.

Was it helpful?

Solution

Here is some pseudo code for a pattern I like to use in these situations. Maybe it will help you as well.

- (void)viewDidLoad {
     //1. put up some type of progressHud or spinner
     //2. call your NSURL wrapper
     //3. in the completion block of your wrapper, set your datasource variables
          //example: @property (nonatomic,strong) NSArray *listOfData;
     //4. create a custom setter for your datasource that calls tableview reload
     //5. enable a refresh function; like "pull to refresh" or a bar button
     //6. when pull to refresh is tapped or called, just repeat these steps


}
- (void)setListOfData:(NSArray*)listOfData {

       _listOfData = listOfData;
       if (_listOfData) {
           [self.tableView reloadData];
       }
}

As I read your question again, here are a couple more thoughts: the pattern above will work for your initial load, to create the list of people or friends, etc.

If you plan on making another round trip after the cell is tapped, then you have to consider a number of issues. This is similar to a common problem with lazy loading images into tableview cells. There are issues like scrolling to consider - what if the cell is scrolled off the view before the data returns, for example, what if the cell has been reused, now the data is not tied to that cell any longer.

There are many async image libraries available on Github that would be good to look at to see how they solved those issues. Generally they are keeping track of the item in the cell and then checking if the cell is still in view and if so, they set the image.

You have a similar issue to solve. Tap the cell, get the new data, then update the cell. Resizing the cell will require you to reload it.

Look into [tableview reloadRowsAtIndexPaths:(NSArray*) with RowAnimation:(UITableViewRowAnimation)];

hope that helps best wishes;

OTHER TIPS

You should have a "Data Model" which represents the content (that is the cells) of your Table View.

Since you have "rows" in your table view, it makes sense this data model is a kind of array (possibly a NSArray) whose elements keep the data and state of the cell.

The data for each cell should not only have all the "data" properties rendered in your cell (e.g. the label) but also its state:

When a user tabs on a cell it will start an asynchronous task. This task may take a while to finish since it fetches data from a remote server. Think of several seconds, or even longer. You need to keep track of pending update tasks, since your implementation should prevent the user to update a cell again before the corresponding pending update task has been finished.

There are several techniques to accomplish this. One way is to have a property in your "Cell Data" class which reflects this state, for example:

@interface CellModel : NSObject    
@property (atomic) BOOL hasPendingUpdate;
...

When the cell will be rendered, you retrieve the value of the property and render the cell appropriately.

When the update task finishes, it updates its cell model data.

This model update will eventually update your Table View. There are several techniques to accomplish this. You should take care about thread-safety here and the "synchronization" of your Data Model and the table view cells. For example ensure the value of the hasPendingUpdate only changes on the main thread - since otherwise your rendered cell may become out of sync with the data model (not to mention race conditions in case you modify and access the property on different threads without synchronization primitives).

While the cell waits for an update, it should visually indicate this state (using a spinner for example) and disable the action to start an update task.

Very much recommended is a "Cancel" button, which either cancels a certain cell update task or all pending update tasks.

When the user moves away from this view, you may consider to cancel all pending tasks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top