views:

1248

answers:

3

This seems like a typical problem, but I have a UITableView that has identical behavior for two separate data sources. What is the best way of going about designing the class hierarchy to have as little duplication and if/else conditions? The view controller is going to do the same exact thing to both data sources, they're just unique in their data set. Should I have the parent controller just set its data source/respective title?

The same issue is relevant as well when using a UISegmentControl for displaying two views with the same interfaces, but different data sources.

A: 

This sounds like you need to use the bridge pattern. This exact scenario is covered by the book "Design Patterns Explained" by Shalloway Trott.

Hope this helps.

Simon Hughes
+6  A: 

Be careful with your terminology here. A UITableView has something called a dataSource but you seem to be referring, essentially, to two different sets of data.

In the case you're suggesting, in the table's dataSource (the object that adheres to the UITableViewDataSource protocol), I'd have three arrays.

  • currentlyViewedArray
  • datasetOneArray
  • datasetTwoArray

In the dataSource methods, use the currentlyViewedArray as the source of the table's data.

Then, set the currentlyViewedArray to whichever array you want to view:

self.currentlyViewedArray = self.datasetOneArray;
[theTableView reloadData];

You can use the UISegmentedControl to switch between the two arrays.

August
+1  A: 

If your goal is to display levels one by one like this:

First Level Second Level Third Level Fourth Level Fifth Level Sixth Level
Item 1  Screen A Detail View N/A N/A N/A
Item 2  Screen B Screen C Detail View N/A N/A
Item 3  Screen D Screen E Screen F Detail View N/A
Item 4  Screen G Screen H Screen I Screen J Detail View

Check this link: http://www.iphonesdkarticles.com/2009/03/uitableview-drill-down-table-view.html

springrider