How to layout complicated view with lots of tableview cells and autolayout constrainsts where storyboard is failing - ios

I have a view like this
Basically it is lots of static tableview cells , where each cell has horizontal scroll views and stack views. Now trying this in storyboard has slowed down it completely, where adding or deleting a constraint or just changing label text in Xcode 9 takes more than 30 seconds and there is always a spinning beachball.
What are the ways I can reduce system overload or make storyboard a bit faster.
Or is it better to move to laying out views in code. In that case any help / advice in this regard will be highly welcome.
Thanks in advance for your efforts.
Here is the link to GitHub repo for the same

You should use a few prototype cells, instead of repeating static cells that are virtually identical. You have at most 6 unique types of cells here. For example, Inputs A, D, E are the same. Inputs B, C, F are the same.
It's beyond the scope of the question to explain how to do this in detail, but the basic procedure is as follows.
Let's say you want to add a prototype cell that you can use for Inputs B, C and F. This is your most basic input cell that will have one text field.
Add to your project a new class that inherits from UITableViewCell.
Name it something like BasicInputCell.
Open your storyboard, select your table view and open the Attributes inspector. Increment the Prototype Cells setting by 1. Xcode will add a prototype cell to your table view in the storyboard
Select the prototype cell and open the Identity inspector. Set the Class name to the name of your cell, BasicInputCell.
Open the Attributes inspector and set the Identifier to the same value so that you can dequeue it by this name.
Add a text field to the cell in the storyboard. Open the Assistant Editor and Control drag from the text field to your class to create an IBOutlet.
Repeat for as many unique cell types as you need. Customize each cell type with different controls on it, such as date picker, as needed.
In your table view controller, dequeue the cells of the type you need based on the index path.

Related

Static table cells with detail

I'm trying to make an iOS 'information' or 'guide' app in Swift. I wanted to utilise the Master Detail template so that when the user clicks a cell it will take them to a relevant ViewController with either basic text labels or a PDF file.
From what I understand, Dynamic Cells can be dynamically changed whilst the app runs however I want to set static cells from the storyboard (or programmatically) and their relevant content/PDF files so the user can view each one. I don't want any "new cell" or "editing" functionality.
How can I make this work? I would post my code so far but it's almost identical to the Master Detail template so I don't see any use. I know this is quite a vague question but I need help and don't know where to start.
Any help will be so appreciated!
A static table cell can entirely be created via storyboard. In the storyboard once you add a tableviewcpntroller you can prototype call added to it.You can add as many prototype cells to your table ( not necessarily every one should have common layout). After all cells are added you can assign individual tags to the cells if you need identify these cells from your code.You can add segue from each cells to move to different page.
Let me know if something more is needed.
In Interface Builder set the Content of the table view to Static Cells.
Drag as many table view cells as you need into the table view.
In the controller create IBOutlets for the labels and the other UI elements.
Connect the outlets in Interface Builder.
Rather than using the datasource methods assign the values directly to the UI elements via the outlets.

Issue with programmatically added buttons

I have a cell in the UITableView. The cell has a UITableViewCell class. In this class I programmatically add few buttons.
Note: the cell is located in the very end of the table view.
Issue:
If I'm using simulator iphone 5 > the buttons will be shown.
But if I'm using simulator iphone 6+ > the buttons will NOT be shown.
Note: when i'm opening it on iphone 5 I need to scroll to see that particular cell. on iphone 6+ i don't need to scroll there, the cell is visible right away.
So I think the issue is somewhere here. May be with the place where I do my 'buttons adding'.
Where should I call the method that sets up the buttons?
Or how can I solve this problem?
TL;DR: If you have different cell types in your table view, you should make sure they have different identifiers.
Explanation:
Table cells are re-used within the UITableView and when the table is first being displayed it won't have any re-usable cells yet. It gets the cells to display by calling on its data source through the UITableViewDataSource protocol.
The thing providing the cells will first ask the table view if it has any re-usable cells (of a particular type), before creating a new one. The way it determines the particular type is from the reuseIdentifier in the table cell.
So, if you have two different classes of table cell, but they share the same reuseIdentifier you will sometimes get one class and sometimes the other - if you get the one without buttons when you're expecting the buttons then this will be a problem!
The answer is to make sure your different cell types have a unique reuseIdentifier.

Swift custom cells layout TableView decision

I need to display a table with in my iPhone app:
neither the number of cells nor the contents are known at compile time, but only at run time.
Views for each cell may differ, one cell has textField and another may have some other view control.
Should I consider Static or prototype cells?
Should I consider tableViewController or viewController with tableview in it?
Any thing I need to consider before I start coding? Thanks in advance
For The issue of dynamic Number of cell at Run time, you can call reload data for table view at any time you have the data source ready.
Prototype Cells should be used with no problem.
Simple Table View will be sufficient for the task.
You have to make cell, either in code or in storyboard, for each type of cell you want, 1 table View can have multiple types of prototype cells, Just name them differently and then make the objects of only the specific cell of which the data is best suited.
It is not that difficult but do handle the data source with extreme care.
Should I consider Static or prototype cells?
If you know all possible subview combinations that your cells might need to display the data appropriately, and they are relatively few, make one prototype for each. for example:
One text field,
Two labels,
One image view and a label,
...etc.
Otherwise, just use the plain-vanilla UITableViewCell (no subclassing) and remove/add subviews at runtime when reusing them (that is, inside the method -tableView:cellForRowAtIndexPath:).
Should I consider tableViewController or viewController with tableview
in it?
The only reason I would choose UIViewController + UITableView over UITableViewController is if -for example- I needed the table view to only take up part of the main view's bounds/screen, and display some other subview in the remainder. Otherwise, you get so much "for free" with UITableViewController that there's just no point in implementing all of that from scratch.
You have to choose prototype cell, u can create different types of cell depending upon your requirement.Any think ok for u, u can create tableview controller or view controller.

Why can't I edit multiple dynamic prototypes in UITableView?

I'm creating a Table View in Interface Builder (Storyboard). I'd like to have a couple of different Dynamic Prototype cells with different sets of Labels and Images in them and so on, and I can give them different reuseIdentifiers so I can pick which ones I want at runtime.
In Interface Builder, I create several Dynamic Prototype cells in my UITableView, which is controlled by a UITableViewController.
In the first cell, I drag and drop in various views and so on.
In the second cell, IB will not let me drag any views into it? I can resize the second cell vertically, but can't put anything into it at all, either by dragging into the cell or into the object graph in the left-side bar.
If I copy and paste the first cell, a second Dynamic Prototype will appear with all of the same contents, but I won't be able to modify the copied cell (can't add or move subviews). However--and this is strange--I can select the constraints and modify their values to resize and shift objects in the second cell.
As a note, running XCode 5-DP3. Tried restarting it (didn't expect that to help, and it didn't). Otherwise, unsure what to try, and unsure if I'm doing something very braindead, or if this is a bug I need to report to Apple.
So, am I crazy? Has anyone experienced this/can anyone recreate this?
EDIT:
After further testing, if I stick a big UIView into the first cell, and then copy that cell, I can edit inside my added view. (Does this make sense?) I can't edit anything that lies within the second UITableViewCell, but if it contains a UIView copied over from the first cell, I can put new views into that view and move them around and so on. Super-strange.
For the sake of posterity, I'm answering my own question:
The way I solved this was to take a UITableViewCell object from the Object library and drag it onto the UITableView. Sounds simple, right?
The problem I was running into was only if I copied existing dynamic prototypes through Cmd+C & Cmd+V, or by incrementing the number in the Attributes inspector for the table view. The Storyboard Editor wouldn't allow me to modify those ones.
Dropping in new cells from the Object library let me tweak them all separately.
XCode 5-DP6 solved issues with not abling to resize cell's subviews.

how do I set up a table view in interface builder for a 'settings' type interface

So I guess the table view has been designed around the idea of using dynamic data received at run time, but I want to make a settings type app where all of the cell values are known as I build it, so I was hoping to be able to set the section / row configuration, labels ect, as well as the properties for each cell (whether it has other views inside it like a switch, or whether it is a button etc) all in interface builder.
can I do that? how?
I am using xcode 4.4.1
To use static cell content you have to make sure that you are adding a UITableViewController not just a UITableView dragged into a UIViewController. The latter requires dynamic prototypes.
Once you've added the table view controller to your storyboard it is as simple as selecting "static cells" in the attributes inspector. From there you can drag and drop UITableViewCells and modify them as you wish.

Resources