gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 06:14:38 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com