如何为具有动态单元格宽度的表格视图单元格内部的单元格使用UICollectionView动态高度?

时间:2018-10-16 10:38:16

标签: ios swift uitableview uicollectionview

My ALL Swift  file codes


import UIKit

class ViewController: UIViewController {
    
    @IBOutlet var tblViewNeel: UITableView!
    
    
    
    var categories = ["Action", "Drama", "Science Fiction", "Kids", "Horror"]
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
 
    }
}

extension ViewController : UITableViewDelegate { }

extension ViewController : UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return categories[section]
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return categories.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CategoryRow
        
        cell.intSec = indexPath.section
        
        return cell
    }
    

    
}

class CategoryRow : UITableViewCell {
    @IBOutlet weak var collectionView: UICollectionView!
    
    
    var intSec = 0
    
    
}

extension CategoryRow : UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 12
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "videoCell", for: indexPath) as! VideoCell
        return cell
    }
    
}

extension CategoryRow : UICollectionViewDelegateFlowLayout {
    
//    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
//        let itemsPerRow:CGFloat = 4
//        let hardCodedPadding:CGFloat = 5
//        let itemWidth = (collectionView.bounds.width / itemsPerRow) - hardCodedPadding
//        let itemHeight = collectionView.bounds.height - (2 * hardCodedPadding)
//        return CGSize(width: itemWidth, height: itemHeight)
//    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        
        if intSec == 0{
            
            return CGSize(width: 100, height: 50 + 30)
        }else if intSec == 1{
            
            return CGSize(width: 150, height: 200 + 30)
        }else if intSec == 2{
            
            return CGSize(width: 200, height: 200 + 30)
        }else if intSec == 3{
            
            return CGSize(width: 250, height: 300 + 30)
        }else if intSec == 4{
            
            return CGSize(width: 300, height: 350 + 30)
        }else if intSec == 5{
            
            return CGSize(width: 350, height: 500 + 30)
        }else{
            
            return CGSize(width: 50, height: 200 + 30)
        }
        
        
    }
}

import UIKit

class VideoCell : UICollectionViewCell {
    
    @IBOutlet weak var imageView: UIImageView!
}

我所有的Swift文件代码

import UIKit

class ViewController: UIViewController {

    @IBOutlet var tblViewNeel: UITableView!

    var categories = ["Action", "Drama", "Science Fiction", "Kids", "Horror"]

    override func viewDidLoad() {
        super.viewDidLoad()

        tblViewNeel.rowHeight = 50.0            
        tblViewNeel.estimatedRowHeight = UITableViewAutomaticDimension
    }
}

extension ViewController : UITableViewDelegate { }

extension ViewController : UITableViewDataSource {

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return categories[section]
    }

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

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CategoryRow

        cell.intSec = indexPath.section

        return cell
    }
}

class CategoryRow : UITableViewCell {
    @IBOutlet weak var collectionView: UICollectionView!

    var intSec = 0
}

extension CategoryRow : UICollectionViewDataSource {

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 12
    }

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

extension CategoryRow : UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {


        if intSec == 0{

            return CGSize(width: 100, height: 50 + 30)
        }else if intSec == 1{

            return CGSize(width: 150, height: 200 + 30)
        }else if intSec == 2{

            return CGSize(width: 200, height: 200 + 30)
        }else if intSec == 3{

            return CGSize(width: 250, height: 300 + 30)
        }else if intSec == 4{

            return CGSize(width: 300, height: 350 + 30)
        }else if intSec == 5{

            return CGSize(width: 350, height: 500 + 30)
        }else{

            return CGSize(width: 50, height: 200 + 30)
        }


    }
}

import UIKit

class VideoCell : UICollectionViewCell {

    @IBOutlet weak var imageView: UIImageView!
}

enter image description here

https://www.thorntech.com/2016/01/scrolling-in-two-directions-like-netflix-part-2-making-api-calls/

我正在使用xcode 9.4.1 我使用了这个演示并且能够具有相同的大小集合视图单元格,但是我想要具有图像及其标题的单元格的动态宽度和高度,如图所示。它对于单元格indexpath == 0很好...但是当我增加了单元格的高度和宽度,标签消失了。欢迎所有建议,并先谢谢.. :)

1 个答案:

答案 0 :(得分:1)

我建议您使用一致的像元大小,并使用以下方法使图像以正确的比例填充大小:

 imageView.contentMode = .scaleAspectFit // OR .scaleAspectFill
 imageView.clipsToBounds = true