What is good practice for code to dynamically add content to View Controller? - ios

I essentially want to have the following scenario.
Very simply, each entry contains a number label, two header labels and two text fields.
Being new to iOS development, I'm not sure of an intelligent, simple way to do this. What is the best way to go about doing this?

This is a great case for using a UITableView. Create a table and a cell prototype that has all the elements that you need (a number, fields for name and age, etc). Create also a different cell prototype for "add more". That would be the last cell in your table. Increment the number of cells when a new record gets added.
There are multiple tutorials online for UITableView. What you want to look for is how to set up and use 2 different cell types. See, for example, this SO question.
If you go with a table view as opposed to just adding some subviews, you get some useful features for free:
scrolling, if you have a lot of records
animated insertion of rows
support for deleting rows/records
easy customization of your list of records: headers, footers, sections.

You can do this manually - there is no problem. Create UITextField, UILabel etc objects in the code, configure them to your liking and add them using
[view addSubview: ]
method

I would:
Create a scroll view - UIScrollView, set the size of this scroll to the size of your main view and add this scroll view to your main view.
Create a content view - UIView, set the width of this content view to the with your the scroll view. But the height should be 0. Set the scroll view in step one's setContentSize: to content view's size. Add this content view as a subview of the scroll view.
Create an iVar to keep track of how many buttons set has been used.
Now every time the + button tapped, create buttons, labels, etc., use the iVar in 3 to calculate and adjust the frame for each buttons, labels, etc.
Adjust the content View's frame to make room for the new set of buttons, labels, etc. Update the scroll view's contentSize: to the content view's frame size.
Add those buttons, labels, etc. in step 4 to content View as subviews.

Case A:
If you are going to add limited number of this type of view
Create one viewController, add textField, label etc to view of this view controller.
Create new instance of this viewController when user clicks add button then use addSubview by refering previously added view's y position
Case B : If number is not limited you will need scroll, then tableView is best option
You will need to create custom cell with your labels and textfield
OR
you can do same using prototype cell if you are using storyboard.
When user will click add button do this
//Update your datasoure for new cell
[tableView insertRowsAtIndexPaths:paths
withRowAnimation:UITableViewRowAnimationTop];
You can create add more view and set it as table footer as it will always stay at bottom, like this
table.tableFooterView = assignYourAddMoreViewHere;

Related

How to make the cells inside of a tableview begin lower down?

I have a tableview. Right now the first cell will begin right at the top of the tableview, but I want it to start some number below the top of the tableview so that I can addSubview() in that spot without covering content in the first cell.
Below you can see what I want to achieve.
The rectangle in the center represents the cells, the line at the top represents the top most of the tableview.
Short answer: You don't. The contents of a table view belong to the table view. You should not try to add subviews to a table view.
As #rickramirr says, you either need to position your table view lower on the screen, or use a header view that contains the contents you want.
(If you decide to position your table view lower down, need to either not manage your table view with a UITableViewController, or have a UITableViewController as a child view controller of the screen's main view controller. A UITableViewController is kind of dumb, and only knows how to manage a table view and nothing else.)
I think that maybe you should add a custom header to your table view or put another view above your table view. Here is Apple docs to customize your header: https://developer.apple.com/documentation/uikit/views_and_controls/table_views/adding_headers_and_footers_to_table_sections
All the views that you want to insert between one tableView cell and another must extend UITableViewCells too. You can create custom cells by defining a new xib file containing a UITableViewCell, whose content could be whatever view you want. Then, you must create a swift file that extends UITableViewCell associated with the xib file. Finally you need to record that cell into the table, either programmatically or by manually inserting it into the storyboard within the table. At that point you will decide its position using the UITableViewDelegate "cellForRowAt" method.

How to place UILabel exactly below UITableView?

I have UITableView with height of ≤500. Tableview data comes from database. Below UITableView, there is one label and two radio button. The problem is if data in the UITableView is less than its height, then it shows blank space between UITableView and those two radio button. I want to place those label and radio button exactly below tableview. How should I do this ?
This is how my tableview looks
If the table view's height does indeed vary depending on shown content, you could use UITableView's tableFooterView.
For a bit more context see this post for example.
Alternatively you could do this with basic auto-layout by tying your label and radio buttons (that you place as siblings of the table) vertically to the bottom of the table view.
I could also imagine that you may need a section footer. So you may want to have a look at that too.
If you want it to be scrollable, add it as the last row in your table view. You may also go with table view's footer.
If you want it to be sticky at the bottom in case of more rows, go with auto layouts. You may create outlet of table view's height constraint and adjust that accordingly depending on the number of rows. However, this approach is not appreciated.

how to increase the height of scroll viewheight dynamically in swift 3?

How can i set the constraints that here two table views are using so that both are dynamical and i need to change the height of scroll view so that both table views and it needs to fit the whole screen and my layout will be as shown below in the image
why you are using tableview in scrollview ? It is not good approach to use scrollable entity into scrollview! You can take one tableview and can create multiple cell for every type of your content like payment view, cart view etc. You can use multiple section also as per requirement. For example, your Table details should be your first section, that can contain multiple rows.
By this way your height will be automatically managed by tableview.

How to scroll a tableViewController's view with only static cells inside?

I have a tableViewController which stores a form for the user to input their information. This form is grouped by a couple of static cells instead of dynamic cells. So it doesn't access the delegate method cellForRowAtIndexPath. Also, I have controlled that user can add some of the attachment and keep stacking. These attachments are stored in a single UIView each. The problem is, when more than 6 attachments were added, the attachment view starts to go out of the screen height. How can I make this tableViewController scrollable?
Please tell me if you need me to show any code.
You don't need to set it scrollable.And you should add these views to table footer view and increase its height at the same time.By this,table view will be able to scroll once its height is bigger than that of frame.

Number of Cell prototypes exceeds tableview height

I have a tableview that has a large number of fairly tall dynamic cells. I've tried creating the prototypes for these but I've run out of height room in the view.
I can't seem to extend the height of the table view, (or the view it's in). I also can't create the tableview outside the viewController, give it a larger height, and just link to it.
Suggestions? I know I can create the cells programmatically or from a separate nib, but I'd really like to do it via storyboard.
thanks,
Just figured this out myself. Try this:
Double-click the table almost anywhere except where there's an existing control. You can also double-click on the outer edge of the table.
Note that the table view will show a highlighted section that aligns with the cell you've clicked. You've entered some sort of selection mode.
Now use the mouse to scroll up or down. The cells will shift up or down as if you were running the app.

Resources