0

Learning swift..so I come across a scenario and not sure of right approach

I have a View controller in which subviews that load depending on the values in the object that is being passed

For eg: Consider a page where the object has name, phone (home, work, mobile), address, birthdate

Now in my view controller, I want to display fields like below picture

enter image description here

So my model looks like:

struct Info {
  let name: String
  let phone: [Phone]
  let address: [Address]
  let bday: String
 }

For phones info, i need to loop thru phone array and add views appropriately. If that Phone struct has no mobile info then i dont need to display cell.

So I created XIB file and trying to load that custom view

Also I need to add constraints for "view with Home Phone" to "view with Mobile phone" etc.. so that they are aligned and show like picture above

So my question is

  1. How can i add that xib to my viewcontroller

2.Do I have add constraints it manually in code or is there any other way (Can you please gimme a example how i could add same)

If my approach is wrong, please let me know the right way of doing this

Your advice would be more valuable

4
  • 2
    Have you checked UITableview? Commented Mar 20, 2019 at 6:59
  • I agree. It definitely looks like a job for UITableView Commented Mar 20, 2019 at 7:02
  • I have only one object. I additionaly need to parse the object values to array inorder to display table view. Instead I thought using custom view and resusing same view would be more efficient because I can avoid extra parsing Commented Mar 20, 2019 at 7:08
  • if you don't want to more parsing then you can put all value in global variables and then use them accordingly.Here UItableview is best option Commented Mar 20, 2019 at 7:10

1 Answer 1

1

Create UIStackView in your xib or storyboard. As stack view has the in-built feature of adjusting view height by its own. Add all your elements(in your case label for name, phone number, etc..) into the UIStackView in vertical or horizontal order as needed. In your controller, just hide the element which you don't need to display for that particular cell. You should have desired view displayed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.