Ios 如何在另一个单元格的视图中加载一个单元格。?
上面的那个是我的内心细胞。我想在下面的单元格中加载此单元格。内部单元的数量是动态的。 上面那间是我的主要牢房。红色部分是一个UIView。我想在UIView中动态添加innerCell的数量。我尝试了下面的代码Ios 如何在另一个单元格的视图中加载一个单元格。?,ios,swift,uitableview,uiview,Ios,Swift,Uitableview,Uiview,上面的那个是我的内心细胞。我想在下面的单元格中加载此单元格。内部单元的数量是动态的。 上面那间是我的主要牢房。红色部分是一个UIView。我想在UIView中动态添加innerCell的数量。我尝试了下面的代码 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let mainCell = tableView.dequeueReusabl
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!
}
但结果是这样的。我提供了自动布局。当我分别运行这两个单元格时,它符合其内容。。我怎么了
上图就是我试图实现的模型。其中航班数量详细信息是动态的。步骤1:创建FlightDisplayViewUIView
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:创建FlightDisplayCellUITableViewCell
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
}
}
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;
}
}
步骤3:创建mainCellUITableViewCell
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
}
}
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!
}
步骤1:创建FlightDisplayViewView
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:创建FlightDisplayCellUITableViewCell
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
}
}
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;
}
}
步骤3:创建mainCellUITableViewCell
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
}
}
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!
}
对于内部表格视图单元,需要在单元视图中添加tableView,并将tableView单元加载到其中。和内部tableView视图必须具有固定高度。@Aman19ish请查看编辑后的问题。我添加了我期待的结果屏幕。好的。但是对于这个UI,为什么要使用内部单元格。如果您需要tableVIew内部单元格的代码,请将您的邮件id发送给我。结果不是这样的..我想,我没有收到您的问题。但据我所说,内部表格视图单元格不使用自动尺寸标注。内部表格视图单元格不起作用。您需要在单元格视图中添加表格视图,并将表格视图单元格加载到其中。和内部tableView视图必须具有固定高度。@Aman19ish请查看编辑后的问题。我添加了我期待的结果屏幕。好的。但是对于这个UI,为什么要使用内部单元格。如果您需要tableVIew内部单元格的代码,请将您的邮件id发送给我。结果不是这样的..我想,我没有收到您的问题。但据我所说,内部的tableView单元格没有使用自动尺寸标注。它不工作