Load different views depending on the category of data on tap of tableview cell iPhone sdk
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开发者_Python百科 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?
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.
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.
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".
精彩评论