首页 > 解决方案 > 如何为具有动态单元格宽度的表格视图单元格内的单元格使用 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!
}

在此处输入图像描述

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

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

标签: iosswiftuitableviewuicollectionview

解决方案


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

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

推荐阅读