Subclass UITableView with Custom UITableViewCells - ios

What I have: 10+ view controllers using a UITableView with custom UITableViewCell. Each view controllers load different data and forward to different sub-pages, thus they cannot combine together.
The aim of the change is to centralize the management of table view's look & feel in a single custom UITableView subclass. But since the data loaded by each view controller is different, I think the UITableViewDataSource and UITableViewDelegate have to be assigned to its original view controller class.
How do I make the change? Or I am thinking at wrong direction?

A tableview's datasource can be separate and independent from its delegate. So, put all of your appearance configurations in a UITableView subclass that implements its own delegate methods, and then create a separate NSObject subclass for each tableview that is responsible for the datasource implementation.

You could make a superclass for all your view controllers that collects all the common logic.

Related

Collectionview Controller vs Collectionview

I'm beginner at IOS. I don't quite understand what is difference between collection view and collecionviewcontroller and in what cases can I use each of them
UICollectionView inherits from UIScrollView (just another UIView)
UICollectionViewController inherits from UIViewController..
and it implements some protocols.. like UICollectionViewDelegate and UICollectionViewDataSource .. it means everything is already done for you.. and you just have to use it.. but as everything is already done for you.. you may not be able to do some stuff.. like resizing your collectionView ..
if you want full control I recommend you to use your own UIViewController.. and add a UICollectionView as a subview.. so you can place it wherever you want and resize it.. don't forget to implement UICollectionView protocols for delegation and datasource :)
It's based on your requirements. You may change some properties with your View controllers. But when you use as part of its' controllers, you can't change it.
For example you CanĀ“t change UICollectionView Size of UICollectionViewController.
In these case, you must use UICollectionView in viewcontroller. As like this, if you want to customize some property but functionality are same in all.
Here I mention some ref for you: refrence
UICollectionViewController is specialized in managing UICollectionView. Collection view controller is inherited from UIViewController specifically for collection views so if collection views are removed then there is no use for Collection view controller.
Having said that,collection views can be used inside UIViewController whereas UIView can't be added inside UICollectionViewController.

move tableview delegates to different class

I have one view controller containing lots of operations. Most of the views are created dynamically. To separate out code I created a class as a myClass of NSObject where I created a function showMyTableView. This function adds a tableview as subview. but I have to write all tableview delegates inside view controller. How I can move require delegates to myclass?
Tomorrow I may require to add subview containing buttons for which I should write action in separate class.
How I can accomplish same?
your new class should handle data only, not ui.
for different table cellview consider use separate tableviewcell subclasses and provide them data models. let them configure themselves
(By 'table view delegates', I assume you mean your table view's delegate and dataSource?)
You can certainly move this stuff to another class. You would simply set the delegate/dataSource of your table view to an instance of MyClass, in your view controller, like so:
self.tableView.delegate = instanceOfMyClass;
MyClass probably shouldn't be responsible for creating the table view and adding it to your view controller's view though. Your view controller should be responsible for this itself.

Can a property of a UITableView be the UITableView Delegate and DataSource

I have a custom view that's a bit of a hack. Basically it's a UIView with tableView as it's property, with additional views in the tableView that need their own delegates. I need a viewController for the UITableView and can't make the UIView it's delegate according to this SO link Custom UIView as UITableView delegate and datasource?.
Can I make the UITableView have a property of UIViewController and set that UIViewController as the tableView's delegate?
In this case according to OOP, the UITableView has a UIViewController so technically, I could expect this to work. But, I am wondering if down the line somewhere this could create problems since the UITableView and UIViewController are coupled in this way.
You don't need a UIViewController for the UITableView - you just need an object or objects that implement the data source and a delegate protocols. As per the accepted answer on the question you linked to you can use a separate controller class to provide this.
The right answer depends a little on how the table is used with your UIView subclass.
If the table will always have the same content (Say a list of months) and there is no value in exposing or abstracting the properties then you can code the delegate and dataSource inside your UIView subclass or in an assistant class.
If the table content will vary depending on how the UIView is used (say a list of people where you don't know what the list is - friends, relatives, employees...) then it would make sense to simply expose the tableview's datasource (and delegate if necessary) properties via your UIView subclass

UITableView in UIView

I want to create a UITableView in UIView class. Can I? The UITable is nested in the UIView, so I figured, I should put the code of the the table in my custom UIView class, but on the other hand, usually the UIViewController is responsible for the table delegates. So what is best?
UITableView has a data source and a delegate which need to be implemented. These could be implemented separately in different objects or they could be implemented in the same object.
Many times, the UIViewController is used as the data source and delegate object but it's not required. Since you're adding the UITableView to a custom UIView it makes sense create a custom object to handle the data source and delegate.
It's fine to add a UITableView to a custom UIView which is not the UIViewController's view.

iOS - What is the difference between Table View and Table View Controller

In the Object Library of Xcode, there are two options one can use to create table view - table view and table view controller.
What is the difference between the two and when would they be used ?
A TableViewController is a ViewController with a TableView built in. This will have the delegate methods needed already declared and setup. This VC is already a TableView delegate and datasource. It cannot be resized. Upside is ease of use, downside is very limited flexibility.
A TableView is just that a TableView (subclass of UIView). It can be added to a ViewController and resized, used alongside another view based object, etc. The upside is the flexibility, the downside is that you have to setup the delegate and datasource methods yourself (in my opinion, well worth the time to get the flexibility).
One other note is that when using the new Static TableView cells (part of iOS5), you have to use a TableViewController.
The UITableViewController is a subclass of the UIViewController. It already assumes you will have UITableView as your rootView, so you already have access from the code to a tableView (self.tableView). It implements the UITableViewDataSource and the UITableViewDelegate protocol. It also gives you alot of methods for you to override. It allows you to not depend on XIB file, because you already know what you will have (UITableView as a rootView).
The UITableView is just UIView, normally you will have to comply to the protocols I have referenced above in your UIViewController in order to populate (data source) and work with it (delegate), and you probably have to create an IBOutlet for your UITableView.
On one hand you have speed but you are not as flexible as the other path. On the other you have the opposite.

Resources