【问题标题】:How to load one cell inside another cell's view.?如何在另一个单元格的视图中加载一个单元格。?
【发布时间】:2017-12-18 06:16:17
【问题描述】:

以上是我的内心细胞。我想在下面的单元格中加载这个单元格。内部细胞的数量是动态的。

以上是我的主要单元格。红色部分是 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!
}

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

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

【问题讨论】:

  • 对于内部表格视图单元格,您需要在单元格视图中添加 tableView 并将 tableView 单元格加载到其中。并且内部 tableView 视图必须具有固定高度。
  • @Aman19ish 请查看已编辑的问题。我添加了我期待的结果屏幕。
  • 好的。但是对于这个 UI 为什么你使用内部单元格。如果您需要 tableVIew 内部单元格的代码,请将您的邮件 ID 发送给我。
  • 它没有得到这样的结果..
  • 我想,我没有得到你的问题。但据我说,对于 Inner tableView 单元格不使用自动尺寸。(它不工作)

标签: ios swift uitableview uiview


【解决方案1】:

第 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)

    }

}

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

}

第 4 步:在控制器中使用 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!
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多