views:

103

answers:

3

Hi all,

In my aplication, I want a tableview with different cell structure depending upon the category of data that's getting loaded in it [I have different categories like video, editorial etc with different structure of data like video has a single label, editorial has 3 labels etc]. I can load different nib files based on the data coming from xml parser.

Now when the cell is tapped, I want to show its detailed view on a new viewController. So my question is is it possible to use only 1 viewController show different fields depending upon the category of data in the cell. Or do I need to create different viewControllers for each of the categories?

+1  A: 

It depends on what kind of data you have in each of your categories. If the format of the data in each categories is different and you already know it, design viewControllers such that they directly read the data and display it however necessary. The viewController should itself handle the displaying logic.

On the other hand, if all categories are of the same type, you can create multiple instances of the same viewController, use them and release them as necessary.

In your case, it sounds like the second option can be preferred.

Edit: It would be wise to go with different sub-classes of viewControllers keeping design pattern in mind - "Closed for modification and open for extension".

Raj
So you mean I should be having different init methods for different categories in my viewController and I should call the appropriate one depending upon the category, right?
neha
If the layouts of the detailed view for each category is different, you better be off by creating a new subclass of viewControllers for each layout. If the layout is same, you can create multiple instances as I already said, and you probably need to maintain some property which will indicate the category it represents.You may choose to use different init and set this property and make sure that you call the designated initializer from your custom init method.You have both the options open, make sure that the solution is extendible to any possible future changes in requirement.
Raj
+1  A: 

You can write code to do whatever you like. But using the same object to display multiple object types wouldn't be considered good practice, assuming that the elements you will be displaying are different.

In OOP, we normally create different classes to simplify code, and share code using various strategies, of which subclassing is would seem appropriate to this case.

Meaning: create a class to handle the general cases applicable to all the object types you want to display, and subclass for the specific requirements for each type.

Paul Lynch
+1  A: 

You can use single view controller. Only thing is you need to feed the data to be displayed in that view controller.

But keeping separate controllers for different functionality will make your code pretty neat and easy to handle. So It would be better if you keep separate controller for handling the data.

Manjunath
I have almost 13 categories in my class... Creating 13 viewControllers is going to make application loaded, isn't it?
neha
oops! Forgot to tell you that you need to create and destroy them at run time :)
Manjunath