[英]Using dictionary values to populate collectionView's label inside of a tableView
所以我有一個tableView
內部的collectionView
。 我想使用數組中的值來填充每個collectionViewCell
內的每個標簽文本。 如果我在collectionView
cellForItemAt
打印以下代碼, cellForItemAt
得到以下內容(參見圖片)(顯然索引超出范圍):
Print(array[indexPath.row].details.values)
因此,使用照片中的示例,我如何獲得以下信息:
另外,您可能已經注意到它的數組不整齊,是否可以重新排序,所以它:
[“ 1”:1,“ 2”:3,“ 3”:5]
非常感謝任何幫助,非常感謝!
我的陣列:
根據您的要求,這里是代碼
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")
}
}
希望它能解決您的問題。
結果
如果我正確理解的話,那么您會有一個數組,格式有點奇怪
array = [[["2": 3, "1": 1, "3": 5]], [["1":7]]]
由於集合視圖位於表視圖內部,因此我假設您已經實現了NSTableViewDataSource
,然后可以將表視圖的當前行另存為tableView:viewFor:中的屬性,從而獲取與該屬性一起使用的數組。 由於您在數組中有一個數組,因此我們需要獲取該數組中的第一項,然后在鍵與當前indexPath.row匹配的地方過濾該項(字典)
let row = String(indexPath.row + 1)
let item = array[currentTableViewRow][0].filter {$0.key = row}
if !item.isEmpty {
label.text = item.value
}
將其轉換為數組 :
Array(array[indexPath.row].details.values)
並打印此值您將以正確的順序獲得正確的數組。
希望對您有幫助,
謝謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.