UITableViewCell动态布局中的Swift UICollectionView无法更改单元格高度

时间:2017-01-15 16:56:22

标签: uitableview uicollectionview swift3 uicollectionviewcell

在Xcode 8.2.1和Swift 3中,我在UITableViewCell中有一个UICollectionView,但我无法正确布局。基本上我希望能够看到所有的明星。我可以调整星星的大小,但我想解决潜在的问题,扩展蓝色波段。

以下是视图的镜头红色波段是contentView,蓝色波段是collectionView。乍一看,它看起来像collectionView高度是问题,但它实际上被其他东西隐藏。我无法找到或更改隐藏collectionView单元格的内容。

collectionView inside tableView cell

我没有在导航器中嵌入的视图控制器之外使用故事板。

这是所有相关代码。

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")
    }
}

2 个答案:

答案 0 :(得分:4)

您可以使用View Debugging

运行项目,返回Xcode并单击Debug栏中的Debug View Hierarchy按钮。或者,转到Debug \ View Debugging \ Capture View Hierarchy。

enter image description here

现在您可以选择红色视图,Xcode将指示选择了哪个视图。

enter image description here

了解更多信息。查看:View Debugging in Xcode

答案 1 :(得分:1)

具体来说,问题是集合视图框架没有正确设置。这只是在接受的答案中我不知道的重要提示所揭示的。

这解决了它:

self.collectionView.frame = CGRect(0, 0, screenWidth, (screenWidth / 4) * Constants.IMAGE_ASPECT_RATIO)
相关问题