0

I know that adjusting UILabel height and UITableViewCell is probably a pretty standard issue, but I'm finding lots of answers that are based on the storyboard and inspectors, but not by just using Swift 3. I've created a table which covers the screen. The height for the cell is being determined as follows:

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

My tableViewCell has a couple of objects in it, a UIImageView (myImage) and a UILabel (myText), set-up in a custom class. The positioning and sizing takes place in cellForRowAt.

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomCell
        cell.myImage.image = UIImage(named: "MyImage")
        cell.myImage.layer.frame = CGRect(origin: CGPoint(x: 0, y: 10), size: (cell.myImage?.image?.size)!)
        cell.myText.text = myArray[indexPath.row]
        cell.myText.frame = CGRect(x: UIScreen.main.bounds.size.width * 0.25, y: 0, width: UIScreen.main.bounds.size.width * 0.7, height: 90)
        cell.myText.numberOfLines = 0
        return cell
    }

The result is a bunch of stacked cells, overlapping each other. What should I do to adjust the height of the UILabel frame to the amount of text coming from myArray and adjust the cell height so that it's at least the height of myImage or myText?

Shane O'Seasnain
  • 3,534
  • 5
  • 24
  • 31
  • have you use autolayout? – Brijesh Shiroya Feb 23 '17 at 06:24
  • http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights – Brijesh Shiroya Feb 23 '17 at 06:32
  • I am using autolayout and thanks for the link to the earlier question. I miss an example of setting up the constraints for multiple objects programmatically in the other question. Constraints are described either conceptually, e.g. referring to updateConstraints(), or shown using the interface builder. Do you have some more explanation on how to tie the objects like UILabels, UIImageViews, etc. to the cell's contentView and how/when to call updateConstraints? – Shane O'Seasnain Feb 28 '17 at 19:23
  • if my comment or give link by me is helpful to you then upvote my comment.thanks – Brijesh Shiroya Mar 01 '17 at 04:30

1 Answers1

0

You can make multiline label inside table view in Swift 3.

    import UIKit

        private let useAutosizingCells = true

        class TableViewController: UITableViewController {

            fileprivate let cellIdentifier = "Cell"

            // MARK: - Lifecycle

            override func viewDidLoad() {
                super.viewDidLoad()

                //setup initial row height here
                if useAutosizingCells && tableView.responds(to: #selector(getter: UIView.layoutMargins)) {
                    tableView.estimatedRowHeight = 44.0
                    tableView.rowHeight = UITableViewAutomaticDimension
                }
            }

    // MARK: - UITableViewDataSource
   extension TableViewController {

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return detailsModel.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)
        let details = detailsModel[indexPath.row]

        cell.textLabel?.text = details.title
        cell.detailTextLabel?.text = details.description

        if useAutosizingCells && tableView.responds(to: #selector(getter: UIView.layoutMargins)) {
            cell.textLabel?.numberOfLines = 0
            cell.detailTextLabel?.numberOfLines = 0
        }

        return cell
    }

}
Anand Nimje
  • 6,163
  • 4
  • 24
  • 43
  • Thanks. I don't see why number of lines should be dependent on constant useAutosizingCells. From what I've learned so far, constraints for the objects in the cell need to be set first, tying the cell's contentView to the label's lower edge. I'm looking into how it is done. Do you have a code example where you show how to do this? – Shane O'Seasnain Feb 28 '17 at 19:05