[英]Dynamic CollectionView depends on TableViewCell Values in SWIFT 4.0
just starting work with swift and I got the problem that I myself can't be solved it. 刚开始工作很快,我遇到了我自己无法解决的问题。
so here, I got a View that structured like this 所以在这里,我得到了这样的视图
ViewController Structure ViewController结构
and imagine i've a json array like this: 想象一下我有一个像这样的json数组:
{
"SCREENTIME": "15:00",
"FORMAT": "2D"
},
{
"SCREENTIME": "17:00",
"FORMAT": "2D"
},
{
"SCREENTIME": "21:00",
"FORMAT": "2D"
},
{
"SCREENTIME": "12:00",
"FORMAT": "3D"
},
{
"SCREENTIME": "14:00",
"FORMAT": "3D"
}
that 'FORMAT' variable will be placed in TableView while 'SCREENTIME' will be placed in CollectionView. “ FORMAT”变量将放置在TableView中,而“ SCREENTIME”变量将放置在CollectionView中。
more likely need a result like this 更可能需要这样的结果
How to append the data into CollectionView dynamically by TableViewCell? 如何通过TableViewCell将数据动态添加到CollectionView中? been searching for many ways and many source code but most of them are static data and swift 2/3 while am using swift 4.
一直在寻找许多方法和许多源代码,但是其中大多数是静态数据,在使用swift 4时是swift 2/3。
You need to sort your data.. after serialising JSON to Array 在将JSON序列化为Array之后,您需要对数据进行排序。
Your Array data 您的阵列数据
let array = [
[
"SCREENTIME": "15:00",
"FORMAT": "2D"
],
[
"SCREENTIME": "17:00",
"FORMAT": "2D"
],
[
"SCREENTIME": "21:00",
"FORMAT": "2D"
],
[
"SCREENTIME": "12:00",
"FORMAT": "3D"
],
[
"SCREENTIME": "14:00",
"FORMAT": "3D"
]]
Sorting 排序
var sortedArray : [String:[String]] = [:]
for dictionary in array {
if let key = dictionary["FORMAT"]{
if let screenTime = dictionary["SCREENTIME"] {
if sortedArray[key] != nil {
sortedArray[key]?.append(screenTime)
}else{
sortedArray[key] = [screenTime]
}
}
}
} }
Expected out put 预期输出
print("Sorted Array = \(sortedArray)")
print("Number of TableView Cells : \(sortedArray.count)")
for (index, key) in sortedArray.keys.enumerated() {
print("Number of CollectionView Cells for Tableview cellIndex \(index) = \(sortedArray[key]!.count)")
}
Sorted Array = ["3D": ["12:00", "14:00"], "2D": ["15:00", "17:00", "21:00"]]
排序数组= [“ 3D”:[“ 12:00”,“ 14:00”],“ 2D”:[“ 15:00”,“ 17:00”,“ 21:00”]]
Number of TableView Cells : 2
TableView单元格数:2
Number of CollectionView Cells for Tableview cellIndex 0 = 2
Tableview cellIndex 0 = 2的CollectionView单元格数
Number of CollectionView Cells for Tableview cellIndex 1 = 3
Tableview cellIndex 1 = 3的CollectionView单元格数
Appling this to your UI - Please Refer this sample code 将其应用于您的UI-请参考此示例代码
ViewController.swift ViewController.swift
class ViewController: UIViewController,UITableViewDataSource {
var sortedArray : [String:[String]] = [:]
override func viewDidLoad() {
tableView.dataSource = self
let array = [ ["SCREENTIME": "15:00","FORMAT": "2D"],["SCREENTIME": "17:00","FORMAT": "2D"],["SCREENTIME": "21:00","FORMAT": "2D"],["SCREENTIME": "12:00","FORMAT": "3D"],["SCREENTIME": "14:00","FORMAT": "3D"]]
for dictionary in array {
if let key = dictionary["FORMAT"]{
if let screenTime = dictionary["SCREENTIME"] {
if sortedArray[key] != nil {
sortedArray[key]?.append(screenTime)
}else{
sortedArray[key] = [screenTime]
}
}
}
}
}
@IBOutlet weak var tableView: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sortedArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell") as! TableViewCell
let key = Array(sortedArray.keys)[indexPath.row]
cell.labelShowtype.text = key
cell.loadCollectionView(array: sortedArray[key]!)
return cell
}
}
Custom TableView Cell 自定义TableView单元格
class TableViewCell: UITableViewCell,UICollectionViewDataSource {
@IBOutlet weak var labelShowtype: UILabel!
@IBOutlet weak var collectionView: UICollectionView!
var array = [String]()
override func awakeFromNib() {
self.collectionView.dataSource = self
}
func loadCollectionView(array:[String]) {
self.array = array
self.collectionView.reloadData()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return array.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
cell.labelTime.text = self.array[indexPath.row]
return cell
}
}
Custom Collection View Cell 自定义集合视图单元格
class CollectionViewCell: UICollectionViewCell {
@IBOutlet weak var labelTime: UILabel!
}
Storyboard 故事板
Output 输出量
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.