开发者

How can I give the same look to all my different UITableViews through my application?

开发者 https://www.devze.com 2023-04-02 19:16 出处:网络
This should be a pretty basic question, I\'ll be happy even if you can link to some sort of article/tutorial that covers this (or a similar) subject.

This should be a pretty basic question, I'll be happy even if you can link to some sort of article/tutorial that covers this (or a similar) subject.

I have an application with four tabs, each tab containing a navigation controller with a table view. I am not using UITableViewController subclasses, as I want to have a static view on top of some tables and an image background below everything. I am also using different types of custom cells (mostly configured with images and code in -willDisplayCell). As I want the same look and feel for all my tables, I am wondering how could I reuse some code, but I'm having trouble to figure this out. Do I have to create a "base" UIViewController subclass wi开发者_运维百科th a UITableView outlet, and then use this one as my superclass? Or do I have to just implement my UITableViewDelegate in a separate class and use an object of that class as the table view delegate? Or should I use a custom UITableView and subclass that one instead?

I could give more details if needed, but I suppose this is enough for a starting discussion. Thanks for any help!


I would consider subclassing UITableView and setting the dataSource and delegate to self and implement GUI logic inside the class and create a custom logic for getting your data in and out (probably forwarding some dataSource/control delegate functions to one of your VCs).

The layouting code for cells should really be placed inside cell logic! You should consider creating custom UITableViewCell subclasses (and maybe XIBs for them) that contain logic for layouting & reusing. (Maybe a dataObject property that makes the cell read your model?!). You can then use your UITableView subclass to create and manage those cells (e.g. a mapping between model classes and XIB names)

This is probably a way to generate a lot of reusable code where you just need to change details like adding model classes or modifying their layouting, or subclassing model classes or subclassed UITableViewCells.


One possible way to do this is to make a custom view which inherits UITableView. override initWith - (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style and set the GUI that you want to use in table.

Now you can use this class in your application wherever you want.

Now as you are using different cell patterns Will suggest you make one class for each cell and inside you cellForRowAtIndexPath make object of different cell based on some flag or data structure. Read Factory Design Pattern to understand this concept in deep.

Basic flow will be like this.

  1. Alloc a new custom table.
  2. Assign datasource and delegate to same table class OR to class which holds the table.
  3. Load data in table. -> Here while loading data figure out what kind data is being used. Based on data make object of relative cell.

This will make all cell and Table reusable throughout application.

For further details read about Decorator Design Pattern.

Post if need for details.


Well I would indeed say the best thing would be to inherit your UITabBarControllers from a MasterController.

Inside this MasterController viewDidLoad or viewWillLoad do you can write common layout and setup that will be used later on by your subclasses.

This is also much more convenient because you can in your MasterController implement methods to refresh your views accordingly.

It could also be nice to have one basic NIB file loaded trough this MasterController and for which custom layout could be done in the children controllers (for example in the initWithNibName:bundle: method.

I think creating a subclass of UITableView is not a good idea because you are not customizing the behavior of Table View per se but you are customizing the controller that uses it instead.

Hope this helps.

EDIT: The NIB way
If you decide to go with the NIB file you can still go with one MasterController by proceeding as followed.

  1. In your MasterController create the necessary outlets (here I think you only need the TableView but it can be more complexe)
  2. In InterfaceBuilder setup your connections as you would normally do
  3. In your code when you create the UITabViewControllers you will use the [[YourTabBarController alloc] initWithNibName:@"YourMasterNIBFile.xib" bundle:nil]

Because the outlets will be inherited, your class YourTabBarController which will inherit YourTabBarController, will have all the necessary information to correctly build your view.

I hope this is clear enough, I've used this method for one of my applications and time consumed to build that MasterController was definitely worth the pain for all the flexibility it provides.


I do this by creating one base class (either uiviewcontroller or uitableviewcontroller), and having a new init method with Type. For example, if you are creating this in code (non-nib)

  • (id)initWithType:(NSInteger)tabtype;

Make this base class also your delegate/datasource, and implement all required methods. Depending on which tab the viewcontroller was instantiated for, adjust the datasource methods - setup different data to show. Delegate methods are probably still same across your view controllers so nothing changes there.

I normally do UI stuff in code, so you could do static view using viewcontroller with a tableView subview, or you could also put static view on top of UITableView, by adding it to tableView as subview, then override layoutSubviews to adjust the y coordinate to make it look like it is static on one spot. (WWDC2011 session Advanced Scrollview Techniques shows how to do this).

If you are using NIB though, then you have to use initWithNib, then easiest way to do this is subclass the baseclass again into 4 different cases for each of your tab.

Now since your code is factored into baseclass, if you want to make change, then it's only one class to change.


Out of interest, why would you want to have a static view on top of tableview cells?

You can have a background image behind the tableview cells with UITableViewController.

0

精彩评论

暂无评论...
验证码 换一张
取 消