首页 > 解决方案 > UITableview 里面的 UICollectionView,table 应该显示动态值,没有固定值。如何生成这个?

问题描述

我有一个UICollectionView里面的UITableView

就像这张图片

在此处输入图像描述

有3个类别,每个类别下都有一些内容。我使用标签值做到了这一点。但是如果我想要动态类别,那么如何填充?

这是我的代码:

import UIKit

类视图控制器:UIViewController {

var categories = ["Revitalize", "Focus","Motivation"]
var revitalImages: [UIImage] = [UIImage(named: "revitalize1.png")!, UIImage(named: "revitalize2.png")!, UIImage(named: "revitalize3.png")!, UIImage(named: "revitalize4.png")!]
var focusImages: [UIImage] = [UIImage(named: "focus1.png")!, UIImage(named: "focus2.png")!, UIImage(named: "focus3.png")!, UIImage(named: "focus4.png")!]

@IBOutlet weak var tableView: UITableView!


override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    self.tableView.separatorStyle = UITableViewCellSeparatorStyle.none


}


override open var shouldAutorotate: Bool {
    return false
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func viewWillAppear(_ animated: Bool) {
    //        tableView.estimatedRowHeight = 200
    //        tableView.rowHeight = UITableViewAutomaticDimension

    tableView.estimatedRowHeight = 200
    tableView.estimatedSectionHeaderHeight = 75
    tableView.sectionHeaderHeight = UITableViewAutomaticDimension
}

}

扩展视图控制器:UITableViewDataSource,UICollectionViewDataSource,UICollectionViewDelegate {

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

    if collectionView.tag == 0{

        return 4
    }
    if collectionView.tag == 1{

        return 4
    }
    if collectionView.tag == 2{

        return 3
    }



    return 0
}

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

    if collectionView.tag == 0{

        cell.imageView.image = revitalImages[indexPath.row]

    }
    if collectionView.tag == 1{

        cell.imageView.image = focusImages[indexPath.row]
    }
    cell.layer.borderWidth = 1
    cell.layer.borderColor = UIColor(red:222/255, green:225/255, blue:227/255, alpha: 1).cgColor


    return cell

}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CategoryRow
    cell.collectionView.delegate = self
    cell.collectionView.dataSource = self
    cell.collectionView.tag = indexPath.row
    cell.collectionView.reloadData()
    cell.titleLbl.text = categories[indexPath.row]
    cell.contentView.backgroundColor = UIColor.white
    cell.backgroundColor = UIColor.white

    return cell
}

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = UIView()
    headerView.backgroundColor = UIColor.white
    return headerView
}


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

func tableView(_ tableView: UITableView, willDisplayHeaderView view:UIView, forSection: Int) {
    if let headerTitle = view as? UITableViewHeaderFooterView {
        headerTitle.textLabel?.textColor = UIColor.clear
    }

}

}

我想在不使用标签 0,1,2 的情况下动态显示数据,......

标签: iosuitableviewswift3uicollectionview

解决方案


推荐阅读