简体   繁体   中英

iOS: UITableView inside UITableViewCell with dynamic cell height

I want to implement UITableView inside UITableViewCell with dynamic height of cell according to inside UITableView content size. How can I implement this any suggestions? I want layout something like this...

Code work:

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 
} 

在此处输入图片说明

use 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 of UILable from Attributes inspector section of storyboard:

  • lines to 0
  • Line break to word wrap

or you can also set these properties from code:

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

Note - Add constraint properly in table view cell.

You have to do something like this:

  1. In ui Master-Item # is be your section header. So take UILabel and add it as a section header.

  2. Your sub-item is the prtotype-cell cell which contains one label.

Constraints for label inside cell.

  • In your cell first add UIView with constraints as follows:
    1. Top , Bottom , Leading, Bottom to superView as 0 .
  • Now add label and give constraints as follow.
    1. Top , Bottom , Leading, Bottom to UIView as 8 .
    2. Give height constraint as per your requirement.
    3. Give height relationship from = to >= .
    4. Set label property line to 0 from storyboard.

Implement this lines in

//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
}

NOTE Don't forget to bind delegate and dataSource of tableView.

use this code in viewDidLoad()

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

Use this Delegate

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

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

Also set these two properties of UILable.

 @IBOutlet weak var label: UILabel!
  1. label.numberOfLines = 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM