0

I have a UITableView and I would like all the functionality that comes built into the UITableViewController except that I would like to have the delegate be a separate class. The problem is I need to pull an identifying piece of information from my data source in order for my delegate class to function. Should I make UITableView delegate to UITableViewController, which would then pull the appropriate identifying information, and then just call the third class from within didSelectRowAtIndexPath? I just want to structure this in the appropriate way.

I am fairly new to iOS and objective-C, so I feel like there is a solution that everyone would use, but it is not obvious to me.

Alec
  • 1,646
  • 3
  • 19
  • 35

2 Answers2

1

I would make the delegate and the data source both point to your custom class. It will be easier to manage table view events all in one place and you really don't need a UITableViewController. If you separate them, there will most likely be heavy coupling between the delegate and the data source as it sounds like you would need to transfer a lot of information between each of them, which defeats the purpose of dividing them into separate classes.

Justin Meiners
  • 10,754
  • 6
  • 50
  • 92
  • note that some table view features are only available if you are explicitly using a `UITableViewController` (e.g. static cells) – Lvsti Jun 25 '12 at 20:29
  • @Lvsti I was not aware of that, in what context do you mean static cells? – Justin Meiners Jun 25 '12 at 20:33
  • If you use the interface builder, from iOS 5 you can prefill the table with static content, but only if your view controller is a `UITableViewController` subclass. See e.g. http://stackoverflow.com/questions/8639780/uitableview-with-static-cells-does-not-appear – Lvsti Jun 25 '12 at 20:36
  • @Lvsti oh I don't use IB at all so that would make sense – Justin Meiners Jun 25 '12 at 20:37
  • The thing is that there is hardly any information that is passed between them. I simply want to switch from a row index to a string identifier, because the delegate class will have no way to interpret an index. Thank you for all your responses! – Alec Jun 26 '12 at 14:26
0

A strategy that I normally use when I have two table views in the same UIViewController and that can fit your needs is to create an Object class for each tableView for example: MyFirstTableViewManager and MySecondTableViewManager.

Each table view manager is set as the delegate and data source to each tableview.

If I need to display a lot of information on each tableview I create properties on each Table View Manager class which I set from the UIViewController.

This way I split the logic of managing each table view to make it easier and keep it clean and from my UIViewController I can set the information to display on each table view by settings each Table View Manager properties and make them to reload its data when required.

Hope this helps you.

Paul N
  • 1,901
  • 1
  • 22
  • 32