示例
要运行示例项目,请克隆仓库,然后首先从 Example 目录运行 pod install
。
使用
步骤 1:在 Swift 中导入 SNAdapter
模块。
import SNAdapter
TableView
步骤 1:声明一个新的类或结构体,继承自 SNCellableModel
。
struct MyModel: SNCellableModel {
let title: String
}
步骤 2:声明一个新的 UITableViewCell
,继承自 SNCellable
。
class MyCell: UITableViewCell, SNCellable {
func configure(_ object: SNCellableModel?) {
guard let myModel = object as? MyModel else { return }
self.textLabel?.text = myModel.title
}
}
步骤 3:在您控制器中获取 SNTableViewSection
和 SNTableViewAdapter
的引用。
private var mySection: SNTableViewSection<MyModel, MyCell>!
private var myAdapter: SNTableViewAdapter!
步骤 4:在您控制器中调用 viewDidLoad
时初始化 SNTableViewSection
和 SNTableViewAdapter
。
override func viewDidLoad() {
super.viewDidLoad()
///....
// MARK: - call setup section
setupMySection()
///...
}
private func setupMySection() {
mySection = SNTableViewSection<MyModel, MyCell>(items: getMyListData())
myAdapter = SNTableViewAdapter(sections: [mySection])
myTableView.setAdapter(myAdapter)
}
private func getMyListData() -> [MyModel] {
return [MyModel(title: "Item 1"), MyModel(title: "Item 2")]
}
更多示例
CollectionView
第一步:声明一个新的从 SNCellable
继承的 UICollectionViewCell
class MyCell: UICollectionViewCell, SNCellable {
@IBOutlet weak var titleLabel: UILabel!
func configure(_ object: SNCellableModel?) {
guard let myModel = object as? MyModel else { return }
titleLabel.text = myModel.title
}
}
第二步:在您的控制器中获取 SNCollectionViewSection
和 SNCollectionViewAdapter
的引用。
private var mySection: SNCollectionViewSection<MyModel, MyCell>!
private var myAdapter: SNCollectionViewAdapter!
第三步:在 viewDidLoad 中初始化 SNCollectionViewSection
和 SNCollectionViewAdapter
override func viewDidLoad() {
super.viewDidLoad()
///....
// MARK: - call setup section
setupMySection()
///...
}
private func setupMySection() {
mySection = SNCollectionViewSection<MyModel, MyCell>(items: getMyListData())
myAdapter = SNCollectionViewAdapter(sections: [mySection])
myCollectionView.setAdapter(myAdapter)
}
private func getMyListData() -> [MyModel] {
return [MyModel(title: "Item 1"), MyModel(title: "Item 2")]
}
更多示例
要求
- Swift 4.2+
- Xcode 10.0+
- iOS 11.0+
安装
SNAdapter 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中
pod 'SNAdapter'
作者
ahmedAlmasri, [email protected]
许可
SNAdapter 基于 MIT 许可证。更多信息请参阅 LICENSE 文件。