2

I have a UITableViewController that displays data retrieved over the network. I am working to figure out what the best design pattern would be so that I can reuse that same table view, but display data from many potential data sources. In my case I could potentially have upwards of 50 completely different network requests that will retrieve data to put into this table view. I don't want to subclass and have 50 different table views all with just a different network request method. What would be the best way about reusing a single class, but enabling the ability to have the table views data source retrieve data from many places?

5
  • Is there same tableviewcell designing, or different for each list?
    – Mrunal
    Dec 14, 2014 at 8:31
  • The table view would have the same design in every case. Imagine that it's a table filled with news items. The design is the same, but the data in each cell would be different. Then in this case I could potentially have news from 50 different sources that could fill the table.
    – BlueBear
    Dec 14, 2014 at 8:35
  • 1
    Then you can use the same tableview. Once the datasource array is updated, call [tableView reloadData] method and your table will be reloaded with new data.
    – Mrunal
    Dec 14, 2014 at 8:38
  • 1
    Do you want to show items from different sources at the same time? Or are you going to have 50 different views where each view shows only items from a single source? Do all the items have the same attributes, such as a story title, date, description, etc.? Dec 14, 2014 at 8:56
  • @MikeTaverne My situation is the latter of your question. I need to have 50 different views each displaying different data. All items will have the same attributes.
    – BlueBear
    Dec 14, 2014 at 18:38

2 Answers 2

0

Whether you want to actually subclass UITableViewController only depends on the kind of data that you want to display and if you need customized UITableViewCells for it.

If you don't need any customization and you just want to display the data in a simple way, there is no need to subclass UITableViewController at all.

Also, the design pattern you are looking for isn't really required, since UITableViewController itself is designed in such a flexible way that you can easily show data from different data sources (that's what the UITableViewDataSource protocol ist for after all).

In case all your 50 data sources always return an array of objects, you can just implement one version of UITableViewController and let it flexibly pull the data from any of these data sources. Then you can use a property, e.g.

@property (nonatomic, strong) NSArray* data;

within the UITableViewController.

Then you use your web service interface to perform the different kinds of requests when needed and pull the appropriate data. And then, once the new data returns from the web service, you can assign it to your property data and call [self.tableView reloadData] which will cause the UITableView to repopulate with the new contents of data.

Not sure if this exactly solves your issue, please let me know if I didn't get your problem quite right. Maybe you need to elaborate a bit more precisely on your issue as well so that I can provide better help :)

1
  • I definitely know the concepts you're talking about here, but my question is mostly directed towards the end of your post. In my case I have a menu system with different items. When you tap on one it will take you to a new view that has one of these table view controllers and will be instantiated with a specific set of data, lets say sports news. Then another menu item will result in a different instance of the table view, but with comedy news. How do I reuse the table view in this case, but change what data needs to be displayed upon instantiation?
    – BlueBear
    Dec 14, 2014 at 18:36
0

You can definitely use one UITableView in this scenario. The root of the issue is translating data from the server of 50 different versions to a data type (i.e. model) that will be displayed in each UITableViewCell.

You need to do the following:

  1. Figure out the data that will be displayed in the cell (i.e. title, description, author, etc.)
  2. Create a custom class/struct that holds this data (i.e. NewsItem)
  3. Create NewsItem objects based on server data.
  4. Populate your data source.
  5. Match up your model properties with your UITableViewCell.
  6. Invoke tableView.reloadData() when you get new data.

So let's say I find that I have a title, description and author, I could create this struct:

struct NewsItem {
    var title: String
    var detail: String
    var author: String
}

As part of my code that interacts with the server, I create objects:

var newsItem1 = NewsItem(title: "Swift's Amazing", detail: "Here we go into why Swift is....", author: "Dustin Hill")
news.append(newsItem1)

Then when as part of configuring my UITableViewCells, I do the following:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let newsItem = news[indexPath.row]
    let cell = tableView.dequeueReusableCell(withIdentifier: "reuseId")
    cell.textLabel.text = "\(newsItem.title) by \(newsItem.author)"
    cell.detailTextLabel.text = newsItem.detail
    return cell
}

This way, you only have one type of object to display in the cell. The key is to translate what you are getting from the server and store them in a generic data type (which from reading your comments is what you are looking for)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.