views:

613

answers:

2

I'm in a hypothetical situation in which I need to list students in a school. I have one table view controller that has several sections, representing a school. Each school has subsequent students. Now, I have the requirement to give the user the capability to view all students for a particular school by clicking on the school name in a top level view of my navigation controller.

The question here is, do I branch out my current "StudentsViewController" and add complex logic in order to allow it to display an individual School's students, or would you experts recommend a new class to handle that table?

The tradeoffs are rather straight forward in that I can indeed probably put everything in one view controller at the cost of some confusing/complex logic. On the other hand, there will be a great deal of repeated code if I write another controller that handles an individual school's students.

What do the experts recommend on this one?

+1  A: 

I think it would depend on the model you are using to hold your data. Lets say you have an array of arrays, (array of schools, each school holds an array of students.)

In this case, I would stick with one tableController.

The logic doesn't have to be hairy if your model design is simple, and I think it would be cleaner and "more correct" than multiple subclasses in this case. Don't forget anywhere the system passes you an NSIndexPath you have the section and row numbers. (School and Student) indexPath.section and indexPath.row

Ryan Townshend
+2  A: 

I think the simplest thing to do would be to have a single class that handles an array of schools. If that array contains only one item, you can (optionally) have no title displayed for the single section. Otherwise, all sections have titles.

Put all your schools into an array, and when a single school needs to be displayed, stick it into an array by itself, and push that into your view controller.

We do a very similar thing in one of our apps, in basically the same way.

Ben Gottlieb