01R*_*Riv 2 collections uitableview ios uicollectionview swift
我有一个嵌入在表视图中的集合视图,因此它可以垂直和水平滚动.它有四个部分,今天,本周,本月,今年.我无法理解如何使用自己的数据填充每个部分.现在我使用下面的代码填充集合视图,但所有部分都有相同的数据.如何使用自己的数据填充每个部分?
因此,如果我有今天的图像数组,本周的图像数组,本月的图像数组,以及今年的图像数组,如何将图像视图的部分设置为中的项目相应的数组?所以今天应该有todayImageArray中的图像,本周应该有thisWeekImageArray中的图像等等.
此外,我将集合视图的委托和数据源设置为表视图.
任何帮助深表感谢!
let images: [[UIImage]] = [
[image_1, image_2, image_3, image_4],
[image_5, image_6, image_7, image_8],
[image_9, image_10, image_11, image_12],
[image_13, image_14, image_15, image_16]
]
func numberOfSections(in collectionView: UICollectionView) -> Int {
return images.count
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images[section].count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! ImageCollectionViewCell
cell.imageView.image = images[indexPath.section][indexPath.row]
return cell
}
Run Code Online (Sandbox Code Playgroud)
您需要结构化数据源.目前,从您的代码中,您看起来已经调用了一个数组,images并且您正在使用它来提供项目数量以及更新单元格内容.但是,如果您需要单独的部分,则需要对数据源进行建模,并在collectionView中为数据源和委托方法提供逻辑响应.
示例简单数据源:
let simpleDataSource: [[UIImage]] = [
[image1, image2],
[image3, image4],
[image5, image6]
]
Run Code Online (Sandbox Code Playgroud)
在collectionView方法中使用它的示例:
func numberOfSections(in collectionView: UICollectionView) -> Int {
return simpleDataSource.count
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return simpleDataSource[section].count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! ImageCollectionViewCell
cell.imageView.image = simpleDataSource[indexPath.section][indexPath.row]
return cell
}
Run Code Online (Sandbox Code Playgroud)
您还应该阅读UICollectionView的文档.
| 归档时间: |
|
| 查看次数: |
1332 次 |
| 最近记录: |