ItemsDataSource 是 UICollectionView 的通用数据源。
CocoaPods 是 Cocoa 项目的依赖管理器。您可以使用以下命令安装它
$ gem install cocoapods
构建 ItemsDataSource 需要 CocoaPods 1.1+。
要使用 CocoaPods 将 ItemsDataSource 集成到您的 Xcode 项目中,请在您的 Podfile
中指定它
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!
target '<Your Target Name>' do
pod 'ItemsDataSource'
end
如果您使用 CocoaPods 1.5.0+
,您可以将 ItemsDataSource
作为静态库包含进来
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
target '<Your Target Name>' do
pod 'ItemsDataSource'
end
然后,运行以下命令
$ pod install
Carthage 是一个去中心化的依赖管理器,它可以构建您的依赖项并为您提供二进制框架。
您可以使用 Homebrew 安装 Carthage,使用以下命令
$ brew update
$ brew install carthage
要使用 Carthage 将 ItemsDataSource 集成到您的 Xcode 项目中,请在您的 Cartfile
中指定它
github "ItemsDataSource"
运行 carthage update
来构建框架,并将构建的 ItemsDataSource.framework
拖入您的 Xcode 项目。
如果您不想使用任何上述依赖管理器,您可以手动将 ItemsDataSource 集成到您的项目中。只需将 Sources
文件夹中的文件复制到您的项目
CellDescriptor.swift
Groupable.swift
Itemable.swift
ItemsDataSource.swift
SupplementaryDescriptor.swift
像往常一样定义您的模型
import UIKit
import ItemsDataSource
struct Vitamin {
// MARK: - Instance Properties
let name: String
let amount: Double
}
使您的模型遵循 Itemable
协议
extension Vitamin: Itemable {
var itemCellDescriptor: CellDescriptor {
return CellDescriptor(reuseIdentifier: ReuseIdentifier.vitaminCell, configure: configureIngredientCell)
}
}
在扩展中向模型添加 configure
方法
extension Vitamin {
func configureIngredientCell(_ cell: ViataminCell) {
cell.vitaminNameLabel.text = name
cell.backgroundColor = UIColor.randomColor()
}
}
在 ViewController 中注入您的数据源
import ItemsDataSource
import UIKit
final class ExampleViewController: UIViewController {
// MARK: - Injections
public var vitaminsDataSourse = ItemsDataSource(items: [Vitamin](),
cellDescriptor: { $0.itemCellDescriptor })
// MARK: - IBOutlets
@IBOutlet var exampleCollectionView: UICollectionView! {
didSet {
setExampleCollectionViewDataSource()
exampleCollectionView.delegate = self
setExampleCollectionViewLayout()
exampleCollectionView.reloadData()
}
}
// MARK: - Instance Properties
var vitamins = [Vitamin]()
// MARK: - ViewController LifeCycle
override func viewDidLoad() {
super.viewDidLoad()
print("vitamins", vitamins)
}
// MARK: - Helpers
private func setExampleCollectionViewDataSource() {
vitaminsDataSourse.items = vitamins
exampleCollectionView.dataSource = vitaminsDataSourse
}
private func setExampleCollectionViewLayout() {
let layout = CommonFlowLayout(columns: 2,
itemHeight: 200,
inset: 5,
spacing: 0,
lineSpacing: 5)
exampleCollectionView.collectionViewLayout = layout
}
}
// MARK: - UICollectionViewDelegate
extension ExampleViewController: UICollectionViewDelegate {}
有关更多详细信息,请查看 iOS 示例。
在 GitHub issue tracker 上发布问题和功能请求。
ItemsDataSource 在 MIT 许可证下发布。有关详细信息,请查看 LICENSE。