我在 UITableView 内部创建了 UICollectionView,在 UItableview 中填充数组值工作完美,当尝试将数组值填充到 collectionView 中时,在 collectionView 中获得相同的值,我想在 tableView 的所有行中填充第零个索引数组第零行 UItableview 的 collectionview 等等 下面我尝试了一个示例,
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource{
let array = ["product 1","product 2","product 3","product4"]
let array1 = ["product id 1","product id 2","product id 3","product id 4"]
let array2 = [["product id 1","product id 2"],["product id 3","product id 4"],["product id 5","product id 6"],["product id 7","product id 8","product id 9","product id 10","product id 11"]] as [NSArray]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : TableViewCell = tableView.dequeueReusableCell(withIdentifier: "cells") as! TableViewCell
cell.tableviewlabel1.text = array[indexPath.row]
cell.tableviewlabel2.text = array1[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return array2.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell : CollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "collection", for: indexPath) as! CollectionViewCell
let rowValue : NSArray = array2[indexPath.row]
for i in 0..<rowValue.count {
cell.collectionviewlabel1.text = rowValue[i] as? String
}
return cell
}
例如,我想要 array2 值在第 0 行的 collectionview 中的第 0 个索引,以及在 tableview 的第一行的 collectionview 中的第一个索引值等等。
最佳答案
首先你的 array2
必须是这样的:
let array2 = [["product id 1","product id 2"],["product id 3","product id 4"],["product id 5","product id 6"],["product id 7","product id 8","product id 9","product id 10","product id 11"]]
不要在 Swift 中使用 NSArray
。您可以将其写为 [String]、[[String]]。
您的 ViewController
将只有 UITableViewDelegate
和 UITableViewDatasource
方法:
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : TableViewCell = tableView.dequeueReusableCell(withIdentifier: "cells") as! TableViewCell
cell.tableviewlabel1.text = array[indexPath.row]
cell.tableviewlabel2.text = array1[indexPath.row]
cell.arrayForCollectionView = array2
return cell
}
}
然后在您的 TableViewCell
中:
class TableViewCell: UITableViewCell {
var arrayForCollectionView : [[String]]! {
didSet {
self.collectionView.reloadData
}
}
@IBOutlet weak var collectionView: UICollectionView!
}
extension TableViewCell : UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if (arrayForCollectionView != nil) {
return arrayForCollectionView.count
}
return 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell : CollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "collection", for: indexPath) as! CollectionViewCell
let rowValue = arrayForCollectionView[indexPath.row]
for i in 0..<rowValue.count {
cell.collectionviewlabel1.text = rowValue[i] as? String
}
return cell
}
}
如果您遇到任何问题,请告诉我。
关于ios - 如何使用 swift 在 UITableview 的 Collection View 中填充不同的数组值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52757453/