Reputation: 777
So I have a collectionView
inside of a tableView
. I would like to use the values from my array to populate each labels text inside each collectionViewCell
. If I print the code below in collectionView
cellForItemAt
I get the below (see picture) (and obviously index out of range):
Print(array[indexPath.row].details.values)
So using the example from the photo, how can I get the following:
Also as you may of noticed its the array is not in order, is it possible to reorder so its:
["1": 1, "2": 3, "3": 5]
Any help is greatly appreciated, many thanks!!
My Array:
Upvotes: 0
Views: 2006
Reputation: 51973
If I understood it correctly you have an array with this, slightly odd, format
array = [[["2": 3, "1": 1, "3": 5]], [["1":7]]]
Since the collection view is inside a table view I assume you have implemented the NSTableViewDataSource
and then you can save the current row for the table view as a property in tableView: viewFor: and thus get the array to use with that property. Since you have an array within an array we need to get the first item in that array and then filter that item (a dictionary) where the key matches the current indexPath.row
let row = String(indexPath.row + 1)
let item = array[currentTableViewRow][0].filter {$0.key = row}
if !item.isEmpty {
label.text = item.value
}
Upvotes: 0
Reputation: 1373
Based on your requirements here is the code
import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
// Global Variable
var tableView: UITableView!
var dataArray = [["2": 3, "1": 1, "3": 5], ["1":7]]
override func viewDidLoad() {
super.viewDidLoad()
tableView = UITableView(frame: self.view.bounds)
tableView.delegate = self
tableView.dataSource = self
self.view.addSubview(tableView)
tableView.register(TableViewCell.self, forCellReuseIdentifier: "TableViewCell")
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: TableViewCell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath as IndexPath) as! TableViewCell
// Passing data to cellection cell
cell.cellData = dataArray[indexPath.row]
cell.backgroundColor = UIColor.groupTableViewBackground
return cell
}
}
class TableViewCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate {
var collectionView: UICollectionView!
var cellData = [String: Int]()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = UICollectionViewScrollDirection.horizontal
collectionView = UICollectionView(frame: self.bounds, collectionViewLayout: layout)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(CollectionCell.self, forCellWithReuseIdentifier: "CollectionViewCell")
collectionView.backgroundColor = UIColor.clear
self.addSubview(collectionView)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
// MARK: UICollectionViewDataSource
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return cellData.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell: CollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath as IndexPath) as! CollectionCell
cell.textLable.text = String(Array(cellData)[indexPath.row].value) // Please check here is some casting
cell.backgroundColor = .black
return cell
}
}
class CollectionCell: UICollectionViewCell {
let textLable: UILabel = {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
label.textColor = .white
label.translatesAutoresizingMaskIntoConstraints = true
label.textAlignment = .center
return label
}()
override init(frame: CGRect) {
super.init(frame: frame)
setupLayout()
}
private func setupLayout() {
addSubview(textLable)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
I hope it solves your issue.
Result
Upvotes: 2
Reputation: 745
Conver it to Array With this :
Array(array[indexPath.row].details.values)
And print this value You get proper array in proper order.
I hope it will help you,
Thank you.
Upvotes: -1