我在Swift 3.2中编码,而TableView Controller内部有一个TableView(垂直滚动),表格的每一行都有一个CollectionView(水平滚动)。
我不确定它是否可以接受编码练习,但我使我的ViewController类继承UITableViewController
和 UICollectionViewDelegate
和UICollectionViewDataSource
。
现在,我有一个硬编码的图像名称数组,并希望在每个集合视图中水平滚动三个图像,每行都有不同的图像。但是,截至目前,每行显示相同的三个图像(" 11.png"," 12.png"," 13.png"),我是想知道如何解决这个问题。
以下是我的ViewController
课程。
//for the tableView
let event = generateRandomData()
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return event.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
return cell
}
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
guard let tableViewCell = cell as? TableViewCell else { return }
tableViewCell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)
}
//for the collectionView
var images: [String] = [
"11.png", "12.png", "13.png",
"21.png", "22.png", "23.png",
"31.png", "32.png", "33.png",
"41.png", "42.png", "43.png",
"51.png", "52.png", "53.png",
"61.png", "62.png", "63.png",
"71.png", "72.png", "73.png",
"81.png", "82.png", "83.png",
"91.png", "92.png", "93.png",
"101.png", "102.png", "103.png",
]
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
collectionView.isPagingEnabled = true; //makes the horizontal scrolling have no bounce and relatively have a better snap
let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath as IndexPath) as! CollectionViewCell
let myCellImage = UIImage(named: images[indexPath.row])
myCell.eventImage.image = myCellImage
return myCell
}
我是业余编码员,所以非常感谢任何帮助!
答案 0 :(得分:1)
我确信这个问题在某个地方已经有了答案,但由于我无法找到我在这里给出的答案。
你的错误在这里:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
/...
let myCellImage = UIImage(named: images[indexPath.row]) //<< indexPath.row will return 0,1 and 2 for each of your collection view
/...
}
表格视图中的每个集合视图都不会相互通信以形成更大的集体视图。由于为numberOfItemsInSection提供了3的int,并且它们的numberOfSections默认为1(你没有设置任何),每个都将在indexpath(0,0),(0,1)和(0, 2)尝试填充数据时。
您的硬编码阵列是一个包含30个项目的数组。因此,请求cellForItemAt时的每个集合视图只会获得图像[indexPath.row]的0,1和2。您还应该将indexPath.item用于集合视图,indexPath.row用于表视图,如果使用错误,它可能会也可能不起作用。
<强>解决方案
您只需设置集合视图的tag
属性即可标记它的行:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
/...
//Make sure you have a subclass for UITableViewCell that have an IBOutlet that connects to the collection view
cell.collectionView.tag = indexPath.row //Note that this only works in your situation where the table view have only 1 section.
/...
}
然后将数组拆分为2D数组:
var images = [
["11.png", "12.png", "13.png"],
["21.png", "22.png", "23.png"],
["31.png", "32.png", "33.png"],
["41.png", "42.png", "43.png"],
["51.png", "52.png", "53.png"],
["61.png", "62.png", "63.png"],
["71.png", "72.png", "73.png"],
["81.png", "82.png", "83.png"],
["91.png", "92.png", "93.png"],
["101.png", "102.png", "103.png"]
]
最后在集合视图中读取数组,如:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
/...
let myCellImage = UIImage(named: images[myCell.tag][indexPath.item])
/...
}