Define cells for UITableView with non-uniform data
In my app I have a detail screen for Product objects. The UI calls for the product details to be displayed using a grouped table view type interface with 3 sections.
Some of the cells in this table are conditional. For instance, by default the third section should display a single cell that says "Register Product" and should push the registration view when tapped. If the product is already registered then the third section should instead display two cells one for Warranty and one for Servicing Information. These would each go to different screens when tapped. Also, they both need to display some kind of data on the table cell. The warranty cell says when the warranty expires and the Servicing cell says when the next servicing is due.
QUESTION (finnally): What's the best way to define the cells and sections that the table should have in any given situation. Primarily I'm looking for a maintainable way to do this since I already have some ideas about un-maintainable ways to do it.
Should I create some sort of keyed dictionary and add/remove items from it during viewWillAppear based on the Product being displayed? I'm worried the number Switch statements that I would have to use throughout the v开发者_如何学Pythonarious tableView events to check what type of cell is at a given index path.
Any ideas?
Have a look at Matt Gallagher's Tableview Classes. They provide a simple and extensible framework for customizable Tableviews. Cells can be loaded from a NIB or constructed in code. There's a simple interface to provide the data for each cell (- (void)configureForData:(id)dataObject
).
Populating the Tableview is easy:
[self addSectionAtIndex:0 withAnimation:UITableViewRowAnimationNone];
[self appendRowToSection:0
cellClass:[NibLoadedCell class]
cellData:@"This is row 0"
withAnimation:UITableViewRowAnimationLeft];
For persistent storage of the data I recommend to create a plist dictionary and load/save data from there. See Property List Programming Guide. For complex database structures use core data for storage.
精彩评论