LKFlatListView
LKFlatListView 是一个高性能的简单列表组件。
如果需要分组/类/区(section),请使用 LKSectionListView
何时使用
- 当有大量结构化的数据需要展现时;
- 当需要对数据进行选择、排序和自定义操作等复杂行为时。
如何使用
- 定义数据模型
数据模型必须实现 Hashable 协议。
class Item: Hashable {
let id: UUID
let title: String
let color: UIColor
let height: CGFloat
init(id: UUID, title: String, height: CGFloat) {
self.id = id
self.title = title
self.color = UIColor(
red: CGFloat.random(in: 0...1),
green: CGFloat.random(in: 0...1),
blue: CGFloat.random(in: 0...1),
alpha: 1.0
)
self.height = height
}
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
static func == (lhs: Item, rhs: Item) -> Bool {
return lhs.id == rhs.id
}
}
- 创建数据源
let dataSource = LKFlatListDataSource<Item>()
var snapshot = dataSource.snapshot()
snapshot.appendItems(
(1...100).map {
Item(
id: UUID(),
title: "\($0)",
height: CGFloat(Int.random(in: 50...300))
)
}
)
dataSource.apply(snapshot, mode: .reload)
- 创建列表项视图
class ItemView: LKListItemView {
// ...
func configure(_ item: Item) {
label.text = item.title
contentView.backgroundColor = item.color
}
}
- 创建列表视图
let listView = LKFlatListView<Item>.flow(
frame: view.frame,
dataSource: dataSource,
inset: .fixed(top: 0, leading: 20, bottom: 0, trailing: 20),
item: LKListFlowItem<Item>(
size: .dynamic { (listView, indexPath, item: Item) in
return CGSize(
width: UIScreen.main.bounds.width / 1 - 40,
height: item.height
)
},,
render: { (itemView: ItemView, indexPath, item) in
itemView.configure(item)
}
)
).onDidSelectItemAt { listView, indexPath, item in
print(">> \(item.id)")
}