How to create simple tableview and load data using Swift in iOS - ios

I am trying to create an app for iOS using Swift but running into problems with the very basics.
To keep it simple I just want the app to initially be a single view application with a button and some sort of list view on the page. I believe a TableView is what is recommended here. When I click the button, I just want it to populate the list/table view with some entries, that's it. To start with, I don't care if these entries are hard-coded, I just want to get something working.
I have been looking at different samples but I am getting confused. Some of them seem to suggest using a TableViewController others don't. When I use a tableview controller, the UI I had created seems to get completely replaced with just an empty tableview list and the button is gone.
I previously have developed apps in Windows phone and found it a lot easier. I'd just add a listview object and in the click method of button, add the items programmatically etc. But this is my first time trying to create an iOS app and it seems a lot more confusing. There are delegates, controllers, views all seemingly needed in order to do something very simple.
Can anyone give me some basic step by step instructions about how to add a tableview to an application and load some data into it through a button click?

Make sure you are clear about the difference between a view and a view controller.
iOS uses the MVC design pattern (Model View Controller).
A view object displays contents to the user and responds to user interaction.
A model object stores state data.
A controller object drives the app logic and mediates between the model and the view.
A UITableViewController is a special subclass of a UIViewController who's job is to manage a table view. It has some extra support in it that makes it a good choice for managing a table view, BUT... there is one annoying thing about it. It is designed so the ONLY view it can manage is a table view. You can't use a UITableViewController if you want to add buttons, labels, and other UI elements to your screen outside of the table view.
What I usually do is to create a create a table view controller, create a separate regular view controller, add a container view to the regular view controller, and then use an embed segue to embed the table view controller inside the view controller. (you just control-drag from the container view to the table view controller.) That way you get the best of both worlds. You may want to create a protocol that the table view controller would use to communicate with it's parent view controller.
You should be able to find a tutorial online on setting up a table view controller as a child of another view controller using container views and embed segues. It's quite easy.

Related

Same TableView in multiple locations swift 3

In my current app I have a tableview that I would like to display on multiple view controllers, I know I could copy and paste the code from one view controller to another but I know enough about coding techniques that copying and pasting large sections of code is bad practice but not enough to know a way to avoid it in this current situation. I am using swift 3 and a standard table view. Does anybody have any good suggestions for me?
If you're using interface builder, you can create a view controller with only your tableview in it and "embed" it in other view controllers using a container view.
IB will create a new embedded view controller when you add the container view but you can delete that and relink the container view to the one containing you tabelview.

Adding a UICollectionViewController inside another UICollectionViewController

I am writing a project where I need to display a list of players (avatars) in a horizontal collection view controller / list, let's call this a HUD.
I want this HUD to be common, that is to say: I know this one component will repeat many times throughout the app.
I do not want to write the Collection View code over and over again.
I want to add the collection view to an existing view controller which already has a collection view.
Almost like an in-line Master-detail display where the master does not change from page to page.
Is there a way in Swift where I can instantiate the HUD collection View and append it to whatever View Controller I am in.
I have included a picture.
Thus my query is this:
How do I include a collection view controller inside another View controller that already has a UICollectionViewController?
How do I share this HUD component across multiple screens in the app?
Many thanks
I used a container view to solve my issue. This issue is now closed.

How to pass object from modal child view controller to parent view controller?

I have a store application, which I built by reading Big Nerd Ranch's iOS Programming, and I advanced my application to the another level and I have created another Item Detail View Controller. It is similar to iPhone's Settings.app, it is a table view controller. This table view consists 2 static table view cells with UITextField's. There is no problem about accessing this text fields, however I'm not able to save data taken from them. My table view controller has no idea about it's parent view controller (which is also another table view controller). So with basic - (void)viewWillAppear and - (void)viewWillDisappear methods I couldn't succeed save data. Having read some documentation about protocols and delegates, I couldn't help myself. Also I have no storyboards or .xib files. Just .h and .m files, may seem stupid but I hardcoded all of them. Ideas???
Let me show you what application looks like:

What should be in View Controllers and what should be in Views?

I am at the beginning of developing an iOS app I am having trouble understanding the MVC (Model-View-Controller) design pattern. I thought I had it down but the more I read the more confused I get. I don't know if this should be an iOS specific question or if the same goes for every use of MVC. I am not using storyboards btw, I want to do it all programmatically.
I believe I understand the basic relationship between Controllers and Models, it's the separation of Views and Controllers that I don't get. Let's say I want to create a UIButton and display it on the screen. Should I initiate the button in the Controller or the View? The controller is responsible for what should be displayed, correct? Wouldn't you just call on a View to display that and not worry about creating the button in the Controller? From what I understand, View Controllers are just Controllers and should therefore control the View, not be the View. It seems like most people do just about everything in the View Controllers. I guess my question boils down to what code goes where?
UIViewController is controller part in the MVC pattern of code.
M(Model)
C(ontroller)
V(View)
Controllers handle navigation between different views , etc.
From here you can get more idea : view and viewcontroller
A view is an object that is drawn to the screen. It may also contain other views (subviews) that are inside it and move with it. Views can get touch events and change their visual state in response. Views are dumb, and do not know about the structure of your application, and are simply told to display themselves in some state.
A view controller is not drawable to the screen directly, it manages a group of view objects. View controllers usually have a single view with many subviews. The view controller manages the state of these views. A view controller is smart, and has knowledge of your application's inner workings. It tells the dumb view objects what to do and how to show themselves.
A view controller is the glue between you overall application and the screen. It controls the views that it owns according to the logic of your application
About View Controllers
View controllers are a vital link between an app’s data and its visual appearance. Whenever an iOS app displays a user interface, the displayed content is managed by a view controller or a group of view controllers coordinating with each other. Therefore, view controllers provide the skeletal framework on which you build your apps.
iOS provides many built-in view controller classes to support standard user interface pieces, such as navigation and tab bars. As part of developing an app, you also implement one or more custom controllers to display the content specific to your app.
Not sure about any iOS specifics but from a PHP standpoint controllers are there to get any data that is needed for the view (via models) and then pass that data to the view.
The view is there to render things to the screen only and should have no logic in it.
So from a website point of view if you wanted to display a users name on the screen:
The controller would request the username from the model
The model would get the username and return it to the controller
The controller would then pass the username to the view
And the view would then render the username.
Hope that helps.
To be not specific, but on a upper layer, you can say as following :
You can put controller code in class extending UIViewControllers
You can put view code in class extending UIView
You can put model code in class extending NSObject

Creating a spit view app from single view ios

I have run into a snag with changing a single view style app into a split view style app.
I have made it so I have the detail view set to the correct view controller, but dont know how to set up a table view for the master view. I created the table view as its own file outside of the storyboard and I don't know how to add it to the master view.
Also how should I link the two together in my code? The Table View was displayed as a popover in the single view version of the app, and it calls a method from the View Controller when a cell is selected. How would I implement the code to bind them?
Sorry if this seems noobish, but this is my first app and still learning.

Resources