2017-02-16 62 views
0

我有一个嵌入在表视图中的集合视图,因此它可以垂直滚动和水平滚动。它有四个部分,今天,本周,本月,今年。我无法理解如何使用自己的数据填充每个部分。现在我正在使用下面的代码填充集合视图,但所有部分都有相同的数据。我如何使用自己的数据填充每个部分?独立填充集合视图的每个部分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 
} 

回答

3

您需要一个结构化的数据源。目前,从您的代码看,您似乎有一个名为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 
} 

还应该通过documentation for UICollectionView读取。

+0

感谢您的回答!我一定会再次阅读文档。我试过你的代码,并且它正在用第一组数据更新所有部分图像视图,所以参考你的示例代码,所有部分图像视图将是image1和image2。你有什么想法可能会造成这种情况? – m1234

+0

已更新为添加缺少'numberOfSections'方法,但是如果您正在运行此操作并且每个部分中都显示相同的图像,则表示您没有引用我的示例代码... – garrettmurray

+0

感谢@garrettmurray您添加的一小部分代码有一个影响。但是现在,所有部分都包含数组中的所有数据。所以每个部分都有:image1,image2,image3,image4,image5,image6。我之前已经阅读过关于收集视图的文档,但您的答案确实有助于我更好地理解它们,所以谢谢! – m1234

0

设置有所有的图像,或者更好的结构设计,组织信息更好或图片名称在我的例子,然后使用numberOfSectionsindexPath.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 
}