What is the Swift iOS purpose of dragging datasource and delegate? - ios

http://teachyourselfios.com download v9 open FlowerDetail. Look at story board, look at Master Scene Table View, and remove both Outlets, datasource and delegate. Everything runs fine without the dragging of datasource and delegate. Datasource and delegate in MasterViewController is not programmatically set.
What is the swift iOS purpose of dragging datasource and delegate?

If the view controller is a UITableViewController (or a UICollectionViewController), the setting of these outlets is not necessary. And in this case, the MasterViewController is a UITableViewController.
But sometimes you have a plain old UIViewController and manually add a UITableView (or UICollectionView) to the scene's main view (you do this if you have other controls you want to put on the view in addition to the table/collection view), in which case setting these outlets (or doing it programmatically) is critical. So it's important to know how to do this in those cases where it's needed.

Related

tableView delegate and datasource

I'm learning Swift. Sometimes I see that Main.Storyboard is used to set up the tableView delegate and dataSource (ctrl+click and so on). Sometimes I see that it's done through coding instead like so:
// create the variable for the tableview
IBOutlet weak var someTableView : UITableView!
// setup delegate and datasource
sefl.someTableView.delegate = self
self.someTableView.datasource = self
I do understand how it works with second way. But it's difficult to realize how it works through Main.Storyboard with no IBOutlet setup.
Thank you for your responses!
It works exactly the same way :)
Let me refresh the principle:
You have a class, provided by Apple in this case, that has to work for a lot of scenario. Apple decided to use a kind of inversion of control, called delegation, where the workflow is inverted (hence the name) : instead of the view controller giving order to the tableView by calling methods on it, it is the tableView that goes and fetch its orders from the controller by calling methods on it.
In order to achieve this kind of IoC (inversion of control), the TableView MUST know the "address" of the object it has to ask for its order. Like you have to know your boss' email address to ask him stuff. So, the UITableView class as a property called dataSource that means to store that address.
Now as a ViewController programmer, you have to set this property to be the address of the view controller that will give the order to that tableview.
2 ways of doing it :
in code : in the view controller, you have a property pointing to the tableview (if linked from the storyboard, it is called indeed an IBOutlet, but doesn't have to be) and you set it's delegate property to self. (meaning 'hey tableView, your boss is myself)
Or you do it in the storyboard, because the graphic template for the tableView let you ctrl+drag from the tableView to the ViewController and set the datasource connection. In this case it will be the storyboard who will have to find the address of the tableView (since it is the one creating it, it's kinda easy) and setting it's delegate property to be the address of the view controller (meaning hey tableView, your boss will be this guy)
Either way, the viewController has to be ready to answer all the question from the TableView, so conforming to the UITableViewDataSource protocol.
the delegate scenario is the same.
It is important to understand how the loading of views works in iOS. Your xib will be translated into the hierarchy of views and they are loaded onto the memory. When you make an IBOutlet of those views, you'll have a reference of that loaded view in your code. If you do not create an IBOutlet, it doesn't mean that the view isn't there. It is there and it is in the memory.
When you set the delegate and the dataSource of a tableView and when the tableView is loaded onto the memory, it sets the delegate and the dataSource of the loaded tableView to the class as specified by you. It doesn't matter if you do not have a reference to it.

set size of tableview in ios storyboard

I want my ViewController to be shared by a tableView and a TextView, where the TextView would appear beneath the TableView. But the TableView insists on taking over the entire scene/screen. How do I set the height of the TableView in the storyboard so I can make room for my TextView? (Please notice that I am not asking for a footer, which is actually what I am trying to change from)
The problem is that you are using a UITableViewController, which means that you get a scene with a full-screen UITableView.
To avoid this, use a normal UIViewController scene and just insert the UITableView into the interface manually (and configure its delegate and dataSource to point to the view controller).
UITableViewController is just a convenience. It doesn't do anything for you that you can't do yourself with a normal UIViewController. And in your situation, it is an inconvenience instead of a convenience.
Add a container view to your ViewController and link to an "external" UITableView instead of the provided UIView that comes with it when your drop it into your storyboard.
Send data via the segue, get it back via a delegate.
This way you can freely design your View with very little headaches.

iOS: Table View (with FetchedResultsController) as Subview of another view

I've followed the CS193p on Itunes and am programming now my own app. In the course they have a CoreDataViewController (which is a subclass of TableViewController with all the necessary NSFetchedResults methods in it). With this I got every tableview running quickly. However I want no a view (at the top of my screen) with table view below for only about half the screen.
I've only found tutorials how to embed a normal table view into a normal viewcontroller and hook up the datasource and delegate outlets. But how would I do this with the NSFetchedResultsController?
I guess I have to do it the same and then copy all the functions from the original CoreDataViewController in my own viewcontroller to handle the fetched results?
Or how would i do that?
I have not seen how the CoreDataViewController was implemented. But yes, you would follow the normal step to set the NsFetchedResultsController. Or you can just create an instance of CoreDataViewController and add its tableView view to the bottom half of your view controller's subview.

Merge two viewcontroller's code using addsubview in IOS

I want to break my code into 3 files and them up via addsubview. For ex. i have a masterview, mastreview contains a currentView. CurrentView contains 1 webview and 1 tableview.
Now, i have written all code in one file and it works like a charm. But i want to make it abstract and loosely coupled . So i need a separate file ex. webviewController to implement its delegate and function related to it AND tableviewController to implement its delegate and functions related to it. And add both by addsubview, alloc init in masterview file.
I did it my way,though i was able to addsubview on CurrentView, the problem was my delegate functions are not working properly.
Also, i am confused about tableviewController should inherit UIViewController or UIView or UITableView.
it would be good if anyone can guide or send some link related to it, any example...???
Actually you should have the app crashing if the delegates are not retained somewhere.
If you do link the object with view controller to be it's delegate at Interface Builder, the view controller will be destroyed after it's outlets so you don't care. If you are creating separate class for the delegate, you should care about it's lifecycle, standard classes do not retain their delegates so you have to retain it on the same level where you are retaining the delegated object. Like if you are creating a UITableView subview and you have MyTVDelegate class, you should create the delegate instance, assign it to tableView.delegate and retain as viewController var so that viewController will dealloc both subviews and their delegates when needed.
For the second question, UITableviewController inherits UIViewController as you can see at header files (command+click on UITableviewController), and UITableView inherits UIView. Every viewcontroller should have the root view of UIView and I believe UITableviewController has UITableView as it's root 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