UITableViewCell动态布局中的Swift UICollectionView可以';t改变细胞高度

UITableViewCell动态布局中的Swift UICollectionView可以';t改变细胞高度,uitableview,uicollectionview,swift3,uicollectionviewcell,Uitableview,Uicollectionview,Swift3,Uicollectionviewcell,在Xcode 8.2.1和Swift 3中,我在UITableViewCell中有一个UICollectionView,但我无法获得正确的布局。基本上我想看到所有的星星。我可以调整星星的大小,但我想解决根本问题,扩大蓝带 这是视图的快照,红色带是contentView,蓝色带是collectionView。乍一看,collectionView高度似乎是个问题,但实际上它被其他东西隐藏了。我无法找到或更改隐藏在collectionView单元格中的内容 在导航器中嵌入的视图控制器之外,我不会使用

在Xcode 8.2.1和Swift 3中,我在UITableViewCell中有一个UICollectionView,但我无法获得正确的布局。基本上我想看到所有的星星。我可以调整星星的大小,但我想解决根本问题,扩大蓝带

这是视图的快照,红色带是contentView,蓝色带是collectionView。乍一看,collectionView高度似乎是个问题,但实际上它被其他东西隐藏了。我无法找到或更改隐藏在collectionView单元格中的内容

在导航器中嵌入的视图控制器之外,我不会使用故事板

这是所有相关代码

class WishListsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var tableView: UITableView = UITableView()        
let tableCellId = "WishListsCell"
let collectionCellId = "WishListsCollectionViewCell"

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self        
    tableView.dataSource = self 
    tableView.backgroundColor = .white
    tableView.register(WishListsCell.self, forCellReuseIdentifier: tableCellId)
    self.view.addSubview(tableView)
    tableView.translatesAutoresizingMaskIntoConstraints = false

    tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
    tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    }

        func numberOfSections(in tableView: UITableView) -> Int {        
            return wishLists.count
        }

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

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{        
            let cell = tableView.dequeueReusableCell(withIdentifier: tableCellId, for: indexPath) as! WishListsCell      
            return cell
        }

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

  func tableView(_ tableView: UITableView,willDisplay cell: UITableViewCell,forRowAt indexPath: IndexPath) {

    guard let tableViewCell = cell as? WishListsCell else { return }
    //here setting the uitableview cell contains collectionview delgate conform to viewcontroller
      tableViewCell.setCollectionViewDataSourceDelegate(dataSourceDelegate: self, forSection: indexPath.section)
    tableViewCell.collectionViewOffset = storedOffsets[indexPath.row] ?? 0
}

func tableView(_ tableView: UITableView,didEndDisplaying cell: UITableViewCell,forRowAt indexPath: IndexPath) {

    guard let tableViewCell = cell as? WishListsCell else { return }
    storedOffsets[indexPath.row] = tableViewCell.collectionViewOffset
}
}

extension WishListsViewController: UICollectionViewDelegate , UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: collectionCellId, for: indexPath) as! WishListsCollectionViewCell
    return cell
    }

func collectionView(_ collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                    sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: 100, height: 100)
}

func collectionView(_ collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                    minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 1.0
}

func collectionView(_ collectionView: UICollectionView, layout
    collectionViewLayout: UICollectionViewLayout,
                    minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 1.0
}

}

class WishListsCell: UITableViewCell {

private var collectionView: UICollectionView!
let cellId = "WishListsCollectionViewCell"


    var collectionViewOffset: CGFloat {
        get { return collectionView.contentOffset.x }
        set { collectionView.contentOffset.x = newValue }
    }


    func setCollectionViewDataSourceDelegate<D: protocol<UICollectionViewDataSource, UICollectionViewDelegate>>
        (dataSourceDelegate: D, forSection section : Int) {

        collectionView.delegate = dataSourceDelegate
        collectionView.dataSource = dataSourceDelegate
        collectionView.tag = section
        collectionView.reloadData()
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        collectionView = UICollectionView(frame: contentView.frame, collectionViewLayout: layout)
        collectionView.translatesAutoresizingMaskIntoConstraints = false
        collectionView.backgroundColor = .blue
        collectionView.register(WishListsCollectionViewCell.self, forCellWithReuseIdentifier: cellId)
        contentView.backgroundColor = .red
        self.contentView.addSubview(collectionView)        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class WishListsCollectionViewCell: UICollectionViewCell {

    var imageView: UIImageView

    override init(frame: CGRect) {
        imageView = UIImageView()
        super.init(frame: frame)
        contentView.addSubview(imageView)        
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.contentMode = .scaleAspectFit
        contentView.translatesAutoresizingMaskIntoConstraints = false
        imageView.backgroundColor = .white

        NSLayoutConstraint.activate([

            NSLayoutConstraint(item: imageView, attribute: .width, relatedBy: .equal,
                               toItem: contentView, attribute: .width,
                               multiplier: 1.0, constant: 0.0),

            NSLayoutConstraint(item: imageView, attribute: .height, relatedBy: .equal,
                               toItem: contentView, attribute: .height,
                               multiplier: 1.0, constant: 0.0),

            ])
    }    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
类WishListsViewController:UIViewController、UITableViewDelegate、UITableViewDataSource{
var tableView:UITableView=UITableView()
让tableCellId=“WishListsCell”
let collectionCellId=“WishListsCollectionViewCell”
重写func viewDidLoad(){
super.viewDidLoad()
tableView.delegate=self
tableView.dataSource=self
tableView.backgroundColor=.white
tableView.register(WishListsCell.self,forCellReuseIdentifier:tableCellId)
self.view.addSubview(tableView)
tableView.translatesAutoresizingMaskIntoConstraints=false
tableView.topAnchor.constraint(equalTo:view.topAnchor).isActive=true
tableView.leadingAnchor.constraint(等式:view.leadingAnchor).isActive=true
tableView.bottomAnchor.constraint(equalTo:view.bottomAnchor).isActive=true
tableView.trailingAnchor.constraint(equalTo:view.trailingAnchor).isActive=true
}
func numberOfSections(在tableView:UITableView中)->Int{
返回愿望列表。计数
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回1
}
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableView单元格{
让cell=tableView.dequeueReusableCell(标识符为tableCellId,for:indexPath)作为!WishListsCell
返回单元
}
func tableView(tableView:UITableView,heightForRowAt indexath:indexPath)->CGFloat{
返回100
}
func tableView(tableView:UITableView,willDisplay单元格:UITableView单元格,forRowAt indexath:indexath){
guard let tableViewCell=单元格为?WishListsCell else{return}
//此处设置uitableview单元格包含符合viewcontroller的collectionview delgate
tableViewCell.setCollectionViewDataSourceDelegate(dataSourceDelegate:self,forSection:indexPath.section)
tableViewCell.collectionViewOffset=StoredOffset[indexPath.row]??0
}
func tableView(tableView:UITableView,diEndDisplaying单元格:UITableView单元格,forRowAt indexPath:indexPath){
guard let tableViewCell=单元格为?WishListsCell else{return}
storedOffsets[indexPath.row]=tableViewCell.collectionViewOffset
}
}
扩展WishListsViewController:UICollectionViewDelegate、UICollectionViewDataSource、UICollectionViewDelegateFlowLayout{
func collectionView(collectionView:UICollectionView,cellForItemAt indexPath:indexPath)->UICollectionViewCell{
让cell=collectionView.dequeueReusableCell(带有reuseidentifier:collectionCellId,for:indexath)作为!WishListsCollectionViewCell
返回单元
}
func collectionView(collectionView:UICollectionView,
布局集合视图布局:UICollectionViewLayout,
sizeForItemAt indexPath:indexPath)->CGSize{
返回CGSize(宽:100,高:100)
}
func collectionView(collectionView:UICollectionView,
布局集合视图布局:UICollectionViewLayout,
截面上的最小最小材料间距:Int)->CGFloat{
返回1.0
}
func collectionView(collectionView:UICollectionView,布局
collectionViewLayout:UICollectionViewLayout,
截面的最小线间距:Int)->CGFloat{
返回1.0
}
}
类WishListCell:UITableViewCell{
私有变量collectionView:UICollectionView!
let cellId=“WishListsCollectionViewCell”
var collectionViewOffset:CGFloat{
获取{return collectionView.contentOffset.x}
设置{collectionView.contentOffset.x=newValue}
}
func setCollectionViewDataSourceDelegate
(数据源elegate:D,节段:Int){
collectionView.delegate=dataSourceDelegate
collectionView.dataSource=dataSourceDelegate
collectionView.tag=节
collectionView.reloadData()
}
重写func awakeFromNib(){
super.awakeFromNib()
//初始化代码
}
重写初始化(样式:UITableViewCellStyle,reuseIdentifier:String?){
init(样式:style,reuseIdentifier:reuseIdentifier)
let布局:UICollectionViewFlowLayout=UICollectionViewFlowLayout()
collectionView=UICollectionView(框架:contentView.frame,collectionViewLayout:layout)
collectionView.TranslatesAutoResizezingMaskintoConstraints=false
collectionView.backgroundColor=.blue
collectionView.register(WishListsCollectionViewCell.self,forCellWithReuseIdentifier:cellId)
contentView.backgroundColor=.red
self.contentView.addSubview(collectionView)
}
必需的初始化?(编码器aDecoder:NSCoder){
fatalError(“初始化(编码者:)尚未实现”)
}
}
类WishListsCollectionViewCell:UICollectionViewCell{
var-imageView:UIImageView
重写初始化(帧:CGRect){
imageView=UIImageView()
super.init(frame:frame)
contentView.addSubview(imageView)
imageView.TranslatesAutoResizezingMaskintoConstraints=false
imageView.contentMode=.ScaleSpectFit
contentView.TranslatesAutoResizezingMaskinToc
self.collectionView.frame = CGRect(0, 0, screenWidth, (screenWidth / 4) * Constants.IMAGE_ASPECT_RATIO)