Same TableView in multiple locations swift 3 - uitableview

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.

Related

Is it possible to embed only part of a view controller inside a container view?

I want to create a view controller that will reuse parts of other controllers from my app. Ideally, the new controller will have 3 containers, with one container for the header (c1) and two other containers bellow it (c2 and c3).
So for c1, I am trying to embed only the view from the controller that holds the header, but doing it in storyboard only allows me to embed the entire view controller where the header is. When I try to drag from the container into the header, the whole view controller is highlighted and I cannot select the header alone. I have also tried just copying the header, but it's A LOT of repeated code.
I will also face the same challenge when trying to embed an existing table view in c2. So is it possible to embed just part of a controller inside a container view? I don't have to stick to the storyboard, so if someone could share how they solved this programatically that would also help a lot.
What you want to do is put your reusable views in a .xib file. That is the way to make perfect copies of complex hierarchies of views. Every time the .xib file is loaded you get a fresh copy of the whole view and its subviews as you've designed it in Interface Builder. The loading must be done in code but there are ways to make a superview that does that automatically.
This screenshot demonstrates:
There is no duplication going on here, just three independent copies of the same .xib file, created automatically. They are all in one screen but that's just to make it easy to make the screenshot; these elements could be anywhere in the app's interface.

Swift 3: Grid of table views

I'm sorry that I don't post any code in this questions; I'm relatively new to programming and I'm already failing on how to realize my idea.
What I want to create is an app, where
the onboarding screen is an editable table view with custom table view cells
when selecting one of the cells, another (!) editable table view is opened.
That means that every time when adding a row to the main table view, another table view is created in addition to the existing one, that I can access through tapping on my table view cell. Of course, the main table view and the created table view should both be saved and still be there when restarting the app.
I don't want code, I just want some specific advice from some more advanced programmers on how to make this.
Thank you!
For a grid layout, you'll want to use UICollectionView, not UITableView. Here's the UICollectionView documentation.
As for showing another table view on tap: simply use a UIViewController subclass (or maybe a UITableViewController if you're sure you want to use a table view) to present another view. You'll probably want to use this in conjunction with a UINavigationController. I recommend reading the documentation for UINavigationController to understand this pattern better.
Apple's docs are pretty great; go use them! Familiarize yourself with the view controller scheme on iOS and a big chunk of the work in learning will take care of itself. Good luck!

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 create simple tableview and load data using Swift in 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.

iOS Storyboards - How to re-use a View Controller

I have an application that takes you through multiple steps (5-6 steps) that at the end of it you will have a newly registered account. I am using a storyboard and a lot of these steps have very similar (or identical) layouts and objects and elements within them. Right now what I am doing is simply creating a new View Controller for each of these steps and then altering the data within them to pertain to the specific step. When I look at my storyboard, I feel like their has to be a way to optimize this so that I don't have so many identical view controllers beside one another.
As you can see there are 5 view controllers that are almost identical and I am looking for a way to condense these to a single view controller or perhaps 5 view controllers that access a single view which contains the variety of elements you see in the image. Any help would be greatly appreciated. Thanks in advance!
If your all view controllers' look is same then no need to create multiple view controllers.
You can only create one view controller and change view or content of view as per requirement.
Hope it helps

Resources