ios - 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 的情况下动态显示数据,......
解决方案
推荐阅读
- c# - json 属性中的默认值空列表
- python - 使用网格数据绘制轮廓时只看到 NAN
- javascript - Vue.js 注意mounted不保证所有子组件也都已经挂载了?
- c# - 如何创建 IList 的实例?
- python - 在 conda 环境中使用 python 找不到包
- python - Is it possible to append lines of code outside a specific function in Python3?
- bash - Linux 测试项目 'nfs_stress' nfs01 'RTNETLINK 答案:不允许操作'
- python - 将列表与字典索引进行比较并根据原始列表顺序返回字典值
- reactjs - 如何在 react-table 上使用 react-perfect-scrollbar 来覆盖默认滚动条?
- java - 使用 Spring MVC 和 Hibernate 每个请求的单个事务