FeedCollectionViewController
一个用于创建数据流以动态加载数据的简单框架。这灵感来源于在 Facebook 或 Instagram 上滚动图片。
图片取自使用颜色代替真实内容的示例项目。
FeedCollectionViewController 是一个用于设置简单流量的通用框架,而 ImageFeedCollectionViewController 则专门用于图像。ImageFeedCollectionViewController 使用了 IDMPhotoBrowser 的分支,所以点击图像可以让您无限滚动图片。
迁移到版本 2
随着版本 2,框架使用委托架构来分离错误情况、数据源和 UI 事件的关注点。这需要
- 按需实现协议
- 在控制器上设置属性。示例项目已更新以反映这一点。对于简单用例,您只需实现
ImageFeedDataSource
协议,然后在您的子类ImageFeedCollectionViewController
中调用
imageFeedSource = self
在 viewDidLoad
或其他适当的地方。下面的 使用方法 部分详细说明了如何使用此框架,以及每个方法移动到了哪里。
示例
要运行示例项目,首先复制仓库,然后从示例目录运行pod install
。示例项目演示了功能,而不使用任何实际内容,它创建彩色图像以说明在使用大量内容时的应用。
安装
FeedCollectionViewController通过CocoaPods提供。要安装FeedCollectionViewController,只需将以下行添加到您的Podfile中
pod "FeedCollectionViewController"
要安装ImageFeedCollectionViewController,只需将以下行添加到您的Podfile中
pod "ImageFeedCollectionViewController"
使用
设置与UICollectionViewController
相当相似,你必须指定一个重用标识符和应从CellData
实现中获取数据的
FeedCollectionViewController
open class ExampleImplementation: FeedCollectionViewController, FeedDataSource {
override open func viewDidLoad() {
super.viewDidLoad()
feedDataSource = self
}
open func getReuseIdentifier(cell:CellData) -> String {
// specifies the identifier of the cell, this can differ per cell
}
open func getCells(start:Int, callback: @escaping (([CellData]) -> Void)) {
// get new cell data, this does not actually mean the cell is being shown
// call `callback` with the new data. `start` is the query starting position
}
open func loadCell(cellView: UICollectionViewCell, cell:CellData) {
// load the cell since it's now actually shown
}
ImageFeedCollectionViewController
open class ExampleImplementation: ImageFeedCollectionViewController, ImageFeedDataSource {
override open func viewDidLoad() {
super.viewDidLoad()
imageFeedSource = self
}
open func getImageReuseIdentifier(cell: ImageCellData) -> String {
// specifies the identifier of the cell, this can differ per cell
}
open func getImageCells(start:Int, callback: @escaping (([ImageCellData]) -> Void)) {
// get new cell data, this does not actually mean the cell is being shown
// call `callback` with the new data. `start` is the query starting position
}
open func loadImageCell(cellView: UICollectionViewCell, cell:ImageCellData) {
// load the cell (ie. a thumbnail) since it's now actually shown
}
你必须有一个自定义的ImageCellData
实现,它继承自IDMPhoto
,将在照片浏览器中使用。要使用图像URLs,在ImageCellData
中使用super.init(url: imageUrl)
。
要自定义ImageFeedCollectionViewController中的视图,你可以实现SingleImageView
并设置imageFeedPresenter
属性。相关方法与IDMCaptionView
中相同。
open override func setupCaption() {
// Setup caption views
}
open override func sizeThatFits(_ size: CGSize) -> CGSize {
// Return the height of the view, the width will be ignored
}
要接收当显示的照片更改时的更新,设置browserDelegate
属性。这将还提供图像失败的回调,并允许工具栏修改。
还可以通过errorDataSource
属性或errorPresenter
属性提供自定义错误消息和视图,以获得对视图的更多控制。
测试
测试是通过FBSnapshotTestCase完成的,在Example/Tests/ReferenceImages_64/FeedCollectionViewController_Tests.Tests
中有测试结果文件。这些是在iPhone SE模拟器上运行的,但你可以通过在setUp()
中启用recordMode
,然后禁用它再次运行测试来在自己的设备上重新运行。
待办
- 允许通过IBOutlets访问属性。由于`FeedDataSource`有默认实现,所以这种方法在当前配置下看起来有些复杂。看起来我需要将所有内容移动到类中,或者移除`getCellsPerRow`的默认实现。有关更多信息,请参阅此处。
- 更好的测试。目前我只实现了快照测试
- 快照测试基于定时器,类似于EarlGrey可能更适合,但在尝试这样做时我遇到了超时错误
ImageCellData
继承自IDMPhoto
,这使得单元格数据与视图代码相关联,而当它更适合作为“傻瓜”模型时,通常是一个结构体。
作者
Oliver O'Neill
许可证
FeedCollectionViewController 在MIT许可证下可用。有关更多信息,请参阅LICENSE文件。