move tableview delegates to different class - ios

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.

Related

Using the same table view cell in multiple view controllers

I have a custom table view cell class, which I use in several view controller's table views. I am adding a feature that gives the user the ability to delete a table view cell and I'd like to remove the cell from the table view when they do so.
Using a delegate is one way to achieve this, so that when the user deletes the table view cell, the table view cell class informs the view controller, through the delegate, that it was deleted and to remove it from the table view. However, this would mean I'd have to do this for each of my several view controllers, which I don't want to do.
So my question is, how can I implement this in such a way that I only have to write the code to remove the table view cell from the table view once?
Subclass the UITableView. Create a protocol like:
protocol customTableViewDelegate {
func deleteTableViewCell()
}
Then give the subclassed UITableView a variable of this type:
var removeCellDelegate: customTableViewDelegate?
And then instead of adding a normal UITableView to your view controller, add the custom one and set the removeCellDelegate to the view controller. Then implement the function to do whatever you want.

Update #IBOutlets via model

I'm trying to decouple my outlets from the controller in order to migrate the view logic into a view model. Ideally I would like to be able to call an updateUI method from within the controller and have it filter down to the labels and text fields within the storyboard.
How I envisioned it working would be as follows:
Inside my controller:
var updateUI: ViewWithFieldsInside {
return ViewWithFieldsInside.reloadData(newData)
}
Inside my view I would have all the outlets hooked in via a UIView class (linked to from storyboard) and have a reloadData method that would set the values equal to newData
Is this the correct way to handle this via Storyboard implementation or am I missing something?

How to create a base ViewController class that has a UITableView?

I have several View Controllers, all using a UITableView, with identical custom cells. The UITableView Datasource methods are identical too (to the line); all that changes is the data source itself (the array from which the table view is loaded). It seems rather redundant for me to have to copy the exact same code for 5 such View Controllers (not to mention it's a bad coding practice!).
I therefore thought of creating a base (or parent) view controller that extends UIViewController, and have all my following (child) controllers inherit from this base view controller.
My problem is this: My base view controller needs to have a UITableView property, and it also contains the DataSource methods. I cannot get the UITableViews within the child view controllers to refer to the parent class for their DataSource.
Am I on a completely wrong track? Is there a better approach to this problem? Any help is greatly appreciated!
Just have one controller into which you add a property for the data source.
When you segue to the controller or create it set the data source property.
If all the code is identical except the data source array, you only need one controller.
In xcode add a new source file: e.g. MyDataViewerController which inherits UITableViewController.
In that file when created, add a property of the form:
#property NSArray *dataSource;
Click on one of your view controllers in storyboard and click the Class attribute inspector tab. Set the class to MyDataViewerController.
You can now use this controller to show any of your data. You can probably delete all the others.
I've assumed its an array containing the data. Replace this with whatever you use.
Assuming you segue to this controller from somewhere, set the property in the
prepareForSegue function.

How to use a UIView delegate in initialization stage

I have a view controller which has a couple of views. Those views need data from the model and I use the view controller as a delegate to supply them that data.
The problem is that they need some of that data when they are initialized (in initWithFrame/awakeFromNib). At that stage the delegate is not set yet (it is set in the view controller's viewDidLoad which is called after the view is initialized).
I can solve it by just accessing the model directly from the view, but that will create quite a mess if every view would access models directly.
Where can I set the delegate in order to use it in the view's awakeFromNib/init?

Subclass UITableView with Custom UITableViewCells

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.

Resources