Prissy Eve Prissy Eve - 4 years ago 185
Swift Question

set dynamic tableview height constraint in storyboard swift

i am using

tableview
and
autolayout
. I want to display array count based increase the
tableview
height in storyboard. how to do this?

I am using below code but not working

@property(strong,nonatomic)iboutlet nslayoutconstraint *dynamicheight;

self.dynamicheight.constant=[self.array count]*44


kindly help me anyone for this?

https://i.stack.imgur.com/jadT7.png
https://i.stack.imgur.com/dP3tZ.png

Answer Source

Try this-

if you are using tableView cell add below code within cellForRowAt method

  cell.textLabel?.numberOfLines = 0
  cell.textLabel?.lineBreakMode = .byWordWrapping

And also set these properties of UITableView within viewDidLoad

self.tableView.tableFooterView = UIView()
self.tableView.estimatedRowHeight = 120 //Your estimated height
self.tableView.rowHeight = UITableViewAutomaticDimension

Hope it will work :)

Check below complete code:

import UIKit

class ViewController: UIViewController {
@IBOutlet weak var guestTView: UITableView!

var guests: [String]?

override func viewDidLoad() {
    super.viewDidLoad()
    self.guests = ["hi this is john and i am a ios developer gjjgjgjgjg hzsg bnfddf dfbdnvfvd bdhgfjgjfgn ngfjggffgfhgh hdfghfggsgg","hi this is john and i am a ios developer gjjgjgjgjg hzsg bnfddf dfbdnvfvd bdhgfjgjfgn ngfjggffgfhgh hdfghfggsg","hi this is john and i am a ios developer gjjgjgjgjg hzsg bnfddf dfbdnvfvd bdhgfjgjfgn ngfjggffgfhgh hdfghfggsg","hi this is john and i am a ios developer gjjgjgjgjg hzsg bnfddf dfbdnvfvd bdhgfjgjfgn ngfjggffgfhgh hdfghfggsg","hi this is john and i am a ios developer gjjgjgjgjg hzsg bnfddf dfbdnvfvd bdhgfjgjfgn ngfjggffgfhgh hdfghfggsg","hi this is john and i am a ios developer gjjgjgjgjg hzsg bnfddf dfbdnvfvd bdhgfjgjfgn ngfjggffgfhgh hdfghfggsg","hi this is john and i am a ios developer gjjgjgjgjg hzsg bnfddf dfbdnvfvd bdhgfjgjfgn ngfjggffgfhgh hdfghfggsg"]

    self.guestTView.estimatedRowHeight = 70.0
    self.guestTView.rowHeight = UITableViewAutomaticDimension
    self.guestTView.tableFooterView = UIView()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
 }

}

extension ViewController: UITableViewDelegate, UITableViewDataSource {

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return (guests?.count)!
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel?.numberOfLines = 0
    cell.textLabel?.lineBreakMode = .byWordWrapping
    cell.textLabel?.text = guests?[indexPath.row]

    return cell

 }

}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download