0

I am completely new to ios but I can create a TableView with fixed constraints in the storyboard with dynamic TableView cell. Now I have to create many views like one ImageView, one Button, a lot of Labels and two TableViews in one ViewController. Those TableViews are dynamic which can get values from API, even the values of TableView may come, sometimes may not come so that I should be gone the view if the TableView values are empty.

  1. I think I want to control the height of the TableView according to the values of API.
  2. if I used Tableview.alpha = 0 to hide the view its just hiding but I wanna gone that view.

I had referred this link iOS: Dynamically set UITableView height in Swift

But I have no idea that how to use this code. Please help me. This question is my last hope. TIA.

Rohini G
  • 3
  • 4
  • What you have to do? DO you want to set tableview height using number of cell? – Manish Mahajan May 11 '18 at 09:50
  • My better advice is take height constraint of TableView.... set constraint.constant = 0 if data is empty otherwise set height – Manish Mahajan May 11 '18 at 09:54
  • @Manish yes! exactly!! the second point is okay. what about the first point? can u pls help me? – Rohini G May 11 '18 at 09:55
  • If you make tableview height as number of cell height, then you must put your tableview in Scrollview, cause screen height willl not increase... to whole table view u must use scroll, better follow second comment – Manish Mahajan May 11 '18 at 09:59
  • yeah i must use scrollview but how can i first set the tableview height dynamically? can u offer some code? – Rohini G May 11 '18 at 10:01
  • To calculate height of table. In cellForRow method you need to add each cell height to one Int variable. Make sure cell height calculated only once – Manish Mahajan May 11 '18 at 10:02
  • can u give some example code? – Rohini G May 11 '18 at 10:05
  • func calculateHeightForConfiguredSizingCell(cell: TableViewCell) -> CGFloat { cell.setNeedsLayout() cell.layoutIfNeeded() let height = cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingExpandedSize).height + 1.0 return height } See if this funtion works by passing your cell – Manish Mahajan May 11 '18 at 10:06
  • I will try this and let you know! – Rohini G May 11 '18 at 10:09
  • tableViewHeightConstraint.constant = tableView.contentSize.height Doesn't it work ? – Manish Mahajan May 11 '18 at 10:18
  • tableViewHeightConstraint.constant = tableView.contentSize.height I don't have an idea that where should I use that. if am using that in viewdidload it showing me an error. kindly help me. – Rohini G May 11 '18 at 10:19
  • Function will return height for each cell, for each cell you have to calculate it and need to assign height constraint of table view? But can please tell me your requirement? Its not proper way – Manish Mahajan May 11 '18 at 10:20
  • tableViewHeightConstraint.constant = tableView.contentSize.height call it after you reload tableview or after your set value to tableview – Manish Mahajan May 11 '18 at 10:23
  • Dynamic tableview is my requirement. Sometimes, from API the count of an array is 3, sometimes it will be 2. I should make the tableview according to that values. and also I have to create one more tableview beneath to that which is also a dynamic tableview. understood? if not, I will explain again. it's like Non-scrollable listview in android. – Rohini G May 11 '18 at 10:25
  • My suggestion is take two tableview with height and height constraint. Set height to 0 if you dont have data. What is highest array count? if it will not exceed than 3 then dynmic is best – Manish Mahajan May 11 '18 at 10:29
  • "two tableview with height and height constraint. Set height to 0 if you don't have data." Please provide me the syntax. am new to ios and I have no programmatical idea with your given data. – Rohini G May 11 '18 at 10:35
  • Means In storyboard set height of tableview and take IBOutlet of height constriant and then set hiehgt like yourHeightConstraint.constant = 30 – Manish Mahajan May 11 '18 at 10:37
  • Anyways, got some idea. Thank you for your valuable time @Manish – Rohini G May 11 '18 at 10:42

1 Answers1

0

Following code dynamically set the height using constraint.This Code Should Works.

viewController.Swift

  class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet var tableView: UITableView!
@IBOutlet var tableviewHeightConstraint: NSLayoutConstraint! //This constraint is your tableview height constraint

var arrayItems = ["iOS", "Swift", "Apple", "Program", "output", "code","viewController","tableview"]

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.dataSource = self
    self.tableView.delegate = self

    self.tableView.register(UINib(nibName: "heightTableViewCell", bundle: nil), forCellReuseIdentifier: "heightTableCell")
     self.tableView.reloadData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.arrayItems.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = self.tableView.dequeueReusableCell(withIdentifier: "heightTableCell") as! heightTableViewCell
    cell.label.text = self.arrayItems[indexPath.row]
    // Your Tableview Height (Dynamically Change the height) 
    self.tableviewHeightConstraint.constant = self.tableView.contentSize.height
    return cell
  }
}

heightTableViewCell.swift

 class heightTableViewCell: UITableViewCell {
 @IBOutlet var label: UILabel!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
 }

}

Note: scrollView should unselect the scrolling enabled option in Attribute Inspector

Vignesh J
  • 257
  • 1
  • 2
  • 14