ios - 如何将一个单元格加载到另一个单元格的 View 中。?

标签 ios swift uitableview uiview

Its my inner cell

上面那个是我的内心细胞。我想将这个单元格加载到下面的单元格中。内部单元格的数量是动态的。 enter image description here

上面那个是我的主格。红色部分是 UIView。我想将 innerCell 的数量动态添加到 UIView 中。我试过下面的代码

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

但是结果是这样的。我提供了自动布局。当我单独运行这两个单元格时,它符合其内容。我做错了什么?

enter image description here

上图是我要实现的模型。航类详细信息数量是动态的。

最佳答案

第 1 步:创建一个 FlightDisplayView(UIView)。

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)

    }

}

FlightDisplayView

第 2 步:创建一个 FlightDisplayCell(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
   }

}

第 3 步:创建一个 mainCell(UITableViewCell)。

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

}

mainCell

第 4 步:在您的 Controller 中使用 mainCell。 (3个单元格信息添加为内部单元格)

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

    let cell = tableView.dequeueReusableCell(withIdentifier: "mainCell") as? mainCell

    let information:NSDictionary = [:]
    cell?.setInformation(3, information)
    return cell!
}

Complete View

关于ios - 如何将一个单元格加载到另一个单元格的 View 中。?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47863032/

相关文章:

ios - Admob 广告不显示 iOS

ios - MVP 架构模式示例。 swift

Swift - 在初始化器中调用函数并在外部定义它

ios - 获取用于安装 iOS 应用商店的下载链接

ios - 按最近位置排序 CLGeoCoder geocodeAddressString 结果

ios - 如何让一个常规对象类做自己的事情是 NSManagedObject 的子类?

swift - 从节中移动最后一行时如何删除节标题

iphone - 如何在通过 Storyboard 使用静态内容时更改分组 tableView 标题部分中的字体样式

objective-c - UITableView 一次删除所有行

ios - 如何访问其他类中的对象?