IOS: UITableView inside UITableViewCell with dynamic cell height

I want to implement UITableViewinside UITableViewCellwith a dynamic cell height according to the size of the content UITableView. How can I implement these suggestions? I want something like this ...

Work with code:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    let identifier = "OrderHistoryTVCell" let cell: OrderHistoryTVCell! = tableView.dequeueReusableCell(withIdentifier: identifier) as? OrderHistoryTVCell 
    let tableInnerContentSize = cell.tableInner.contentSize.height 
    let nn = cell.viewAfterTable.frame.height+tableInnerContentSize 
    return nn 
} 

enter image description here

+4
source share
4 answers

use the following:

@IBOutlet weak var tableView: UITableView! //connect the table view

//do following in viewDidLoad method
tableView.estimatedRowHeight = 100 //Your estimated height
tableView.rowHeight = UITableViewAutomaticDimension

Also set these two properties in the "IIILIT" section of the "Attribute Infrastructure" section of the storyboard:

  • rows to 0
  • Line break for word wrap

or you can also set these properties from code:

self.lblxyz.numberOfLines = 0
self.lblxyz.lineBreakMode = .byWordWrapping

Note. Correctly add the constraint to the table cell.

0
source

- :

  • ui Master-Item # . UILabel .

  • - prtotype-cell, .

.

  • cell UIView :
    • , , , superView 0.
  • , .
    • , , , UIView 8.
    • .
    • = >=.
    • label 0 .

//MARK:- TableView

public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat{
    return 60 // return header height
}

public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    // return your section header i.e. master item label

}

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

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

public func numberOfSections(in tableView: UITableView) -> Int{
// return number of section i.e. total count of master item.
}

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
// returns number of cells in each section
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
    // return your custom cell here
    //eg:
    // cell.labelFood.text = your data
}

delegate dataSource tableView.

0

DidLoad()

self.tableView.estimatedRowHeight = 350
 self.tableView.rowHeight = UITableViewAutomaticDimension

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
            return UITableViewAutomaticDimension

        }
-1
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
  {
        self.tableView.estimatedRowHeight = 160
        return UITableViewAutomaticDimension
  }

UILable.

 @IBOutlet weak var label: UILabel!
  • label.numberOfLines = 0
-2
source

Source: https://habr.com/ru/post/1669006/


All Articles