1

WHAT I'M DOING

I'm developing an IOS Application with swift3 (coming from an existing android version). I have a UITableView which can have 3 types of views (as cells). One type will be the header and other represents diferent types (hours, photo or/and material). Take a look at the next image to clarify the schema:

enter image description here

WHAT I TRIED

After days of research, I tried a lot of possible solutions. These are the best intents:

  • Static rows with different views:

https://stackoverflow.com/a/30776750/2131420 This is not a solution, because the number of views are static and i can't have random order of views (the app crashes if order was different... First the view 2, next the view 1, then view 2 again...)

  • One view with all view types inside:

For the moment, this is the best try... This consist in draw one view with the 3 view types inside. One UIView (like container) for each view type. Then, with code, set the height of the views to 0 with the unnecessary UIView (containers). The problem of this method is the automatic dimension of the cells. Researching, i get than you can set NSLayoutConstraints to 0 for hidden. But, if you do it, the text gets cutted with the three points (...) I can't do this. Also, I tried to put the property lineBrake to "WordWrap" and lines to 0. This also does not solve the problem. Image example: enter image description here

THE QUESTION

How can I load different views into cell depending of value from array? And how can I set the height of this cells according to the height of the content?

Sorry for the english and thanks in advance!

batman567
  • 826
  • 2
  • 12
  • 23
Joan Casadellà
  • 276
  • 2
  • 17

1 Answers1

2

Since you want dynamic cells , I would start by making 3 xibs for each cell with its own class since they show different views and have different models from each other.

Make sure that constraints of items inside the cell are adjustable and - only if your app supports versions prior to iOS 7 - return a fixed size inside tableView:heightForRowAtIndexPath: .
Heres an easy tutorial to help you understand them better.

You should implement UITableView delegate method

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

Also, implement tableView:estimatedHeightForRowAtIndexPath: in order to give to the UITableView an estimated size of the cell.

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return 44
    }

In the UITableView delegate method

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

             if indexPath.row == 0 {
               //Show your first cell
             }else if (model[indexPath.row].type == 1) { //or another way to distinguish from third cell
              //Show your second cell
             }else {
              //Show your third cell
             }
 }

Dont forget to add your custom cell to UITableView in viewDidLoad()

@IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
self.tableView.register(UINib(nibName: "FirstCell",bundle: nil), forCellReuseIdentifier: "FirstCell")
self.tableView.register(UINib(nibName: "SecondCell",bundle: nil), forCellReuseIdentifier: "SecondCell")
self.tableView.register(UINib(nibName: "ThirdCell",bundle: nil), forCellReuseIdentifier: "ThirdCell")
}
unniverzal
  • 803
  • 10
  • 17