How to load one cell inside another cell's view.?

时间:2017-12-18 06:16:18

标签: ios swift uitableview uiview

Its my inner cell

The above one is my inner cell. I want to load this cell inside the below cell. The number of inner cell is dynamic. enter image description here

The above one is my main cell. The red colored section is a UIView. I want to add numbers of innerCell dynamically into the UIView. I tried the below code

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let mainCell = tableView.dequeueReusableCell(withIdentifier: "FlightCellMain", for: indexPath) as? FlightCellMain
    let innerCell = tableView.dequeueReusableCell(withIdentifier: "FlightCell", for: indexPath) as? FlightCell


    mainCell?.flightCellView.addSubview(innerCell!)
    mainCell?.backgroundColor = .clear
    innerCell?.backgroundColor = .clear
   // innerCell?.innerCellWidthConst.constant = (mainCell?.mainView.frame.width)!
    self.showStopsView(2, self.airportlist, (innerCell?.leftRound)!, (innerCell?.rightRound)!, (innerCell?.centerLine)!, (innerCell?.routeView)!)
    mainCell?.flightCellViewHieghtConst.constant = (mainCell?.flightCellViewHieghtConst.constant)! + 125
    innerCell?.layoutIfNeeded()
    mainCell?.layoutIfNeeded()
    return mainCell!
}

enter image description here

But the result is getting like this. I provided autolayout. When I run the two cells individually it fits its content.. Whats wrong am I doing ?

enter image description here

The above picture is the model that I'm trying to achieve. Where the number of flights details is dynamic.

1 个答案:

答案 0 :(得分:4)

Step 1: Create a FlightDisplayView(UIView).

HomeRestuTableViewCell.xib

FlightDisplayView

Step 2: Create a FlightDisplayCell(UITableViewCell).

import UIKit

class FlightDisplayView: UIView {

    override init(frame: CGRect) {
       super.init(frame: frame)
       self.commonInit()
    }

    required init?(coder aDecoder: NSCoder) {
       super.init(coder: aDecoder)
       self.commonInit()
    }

    private func commonInit (){

       let xibs = Bundle.main.loadNibNamed("FlightDisplayView", owner: self, options: nil)
       let view = xibs?.first as! UIView
       view.frame = self.bounds;
       self.addSubview(view)

    }

}

Step 3: Create a mainCell(UITableViewCell).

import UIKit

class FlightViewCell: UITableViewCell {

@IBOutlet weak var innerCellView:FlightDisplayView!

   override func awakeFromNib() {
      super.awakeFromNib()

   }

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

    // Configure the view for the selected state
   }

}

mainCell

Step 4: Use mainCell into your controller. (3 cell information added as a inner cell)

import UIKit

class mainCell: UITableViewCell {

    @IBOutlet weak var table: UITableView!
    @IBOutlet var heightConstraintForFlightTableView : NSLayoutConstraint!

    override func awakeFromNib() {
       super.awakeFromNib()
       table.delegate = self
       table.dataSource = self
       table.estimatedRowHeight = 100.0
       table.rowHeight = UITableViewAutomaticDimension
       table.register(UINib.init(nibName: "FlightViewCell", bundle: nil), forCellReuseIdentifier: "FlightViewCell")

       heightConstraintForFlightTableView.constant = 0.0
    }

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

    // Configure the view for the selected state
   }

   func setInformation(_ numberOFFlight : CGFloat, _ information : NSDictionary) {
        heightConstraintForFlightTableView.constant = numberOFFlight * 155.0

    // 155 is fixed flight cell height
       table.reloadData()
   }

}


extension mainCell:UITableViewDelegate,UITableViewDataSource {

   func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       return 3
   }

   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

       let cell = tableView.dequeueReusableCell(withIdentifier: "FlightViewCell")
       return cell!
   }

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

}

Complete View