Ios 独立填充集合视图的每个部分

Ios 独立填充集合视图的每个部分,ios,swift,uitableview,collections,uicollectionview,Ios,Swift,Uitableview,Collections,Uicollectionview,我在表视图中嵌入了一个集合视图,所以它可以垂直和水平滚动。它有四个部分,今天,本周,本月,今年。我很难理解如何用自己的数据填充每个部分。现在我正在使用下面的代码填充集合视图,但所有部分都有相同的数据。如何使用每个部分自己的数据填充每个部分 因此,如果我有今天的图像数组、本周的图像数组、本月的图像数组和今年的图像数组,我如何将其部分中的图像视图设置为相应数组中的项目?所以今天的图像应该在todayImageArray中,本周的图像应该在thisWeekImageArray中,依此类推 我还将集合视

我在表视图中嵌入了一个集合视图,所以它可以垂直和水平滚动。它有四个部分,今天,本周,本月,今年。我很难理解如何用自己的数据填充每个部分。现在我正在使用下面的代码填充集合视图,但所有部分都有相同的数据。如何使用每个部分自己的数据填充每个部分

因此,如果我有今天的图像数组、本周的图像数组、本月的图像数组和今年的图像数组,我如何将其部分中的图像视图设置为相应数组中的项目?所以今天的图像应该在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
}

设置一个数组,该数组包含所有图像,或者更好的结构设计用于更好地组织信息,或者像我的示例中那样包含图像名称,然后使用
numberOfSections
indexPath.section
属性分配这些图像:

let images = [
    ["imageA1", "imageA2"],
    ["imageB1", "imageB2"]
]

override func numberOfSections(in collectionView: UICollectionView) -> Int {
    return images[section]
}

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return images[section].count
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! ImageCollectionViewCell

    cell.imageView.image = UIImage(named: images[indexPath.section][indexPath.row])

    return cell
}

您需要一个结构化的数据源。目前,从您的代码来看,您似乎有一个名为
images
的数组,您使用该数组既可以提供项目数量,也可以更新单元格内容。但如果需要单独的部分,则需要对数据源进行建模,并在collectionView中为数据源和委托方法提供逻辑响应

简单数据源示例:

let simpleDataSource: [[UIImage]] = [
  [image1, image2],
  [image3, image4],
  [image5, image6]
]
在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
}

您还应该通读。

谢谢您的回答!我一定会再读一遍这些文件。我尝试了您的代码,它正在使用第一组数据更新所有截面图像视图,因此参考您的示例代码,所有截面图像视图都将是image1和image2。你知道这可能是什么原因吗?更新后添加了一个缺少的
numberOfSections
方法,但是如果你正在运行这个方法,并且每个部分都有相同的图像,那么你没有引用我的示例代码…谢谢@garrettmurray,你添加的代码有效果。但是,现在所有部分都包含数组中的所有数据。所以每个部分都有:图像1,图像2,图像3,图像4,图像5,图像6。我以前读过关于集合视图的文档,但是你的回答确实帮助我更好地理解它们,所以谢谢你!如果看不到您的自定义单元格代码,很难判断。在我看来,您可能在单元格中的某个位置添加了一个
imageView
,导致每次重复使用单元格时都会将其添加到视图层次结构中。我先看看那里。回答得很好@加里特默里