LayoutKit 简单分析

LayoutKit 简单使用

LayoutKit 官网

创建

使用 tableView / collectionView 初始化 ReloadableViewLayoutAdapter
如: let reloadableViewLayoutAdapter = ReloadableViewLayoutAdapter(reloadableView: tableView)

设置 dataSourcedelegate

1
2
tabView.dataSource = reloadableViewLayoutAdapter
tabView.delegate = reloadableViewLayoutAdapter

关于 talbeView cellForRowAtIndexPath

1
2
3
4
5
6
7
/// - Warning: Subclasses that override this method must call super
open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let item = currentArrangement[indexPath.section].items[indexPath.item]
let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath)
item.makeViews(in: cell.contentView)
return cell
}

关于数据来源

在封装数据的同时, 设置的单元格内的约束

1
2
3
4
5
6
7
8
9
10
11
12
13
// 封装数据
let profileCard = ProfileCardLayout(
name: "ProfileCardLayout.name",
connectionDegree: "ProfileCardLayout.connectionDegree",
headline: "ProfileCardLayout.headline",
timestamp: "ProfileCardLayout.timestamp = 5 minutes ago",
profileImageName: "50x50"
)
let feedItems = [Layout](repeating: profileCard, count: 1000)

reloadableViewLayoutAdapter.reload(width: width, synchronous: synchronous, layoutProvider: { [weak self] in
return [Section(header: nil, items: itemList, footer: nil)]
})

关于单元格布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import LayoutKit

open class ProfileCardLayout: StackLayout<UIView> {

public init(name: String, connectionDegree: String, headline: String, timestamp: String, profileImageName: String) {
let labelConfig = { (label: UILabel) in
label.backgroundColor = UIColor.yellow
}

let nameAndConnectionDegree = StackLayout(
axis: .horizontal,
spacing: 4,
sublayouts: [
LabelLayout(text: name, viewReuseId: "name", config: labelConfig),
LabelLayout(text: connectionDegree, viewReuseId: "connectionDegree", config: { label in
label.backgroundColor = UIColor.gray
}),
]
)

let headline = LabelLayout(text: headline, numberOfLines: 2, viewReuseId: "headline", config: labelConfig)
let timestamp = LabelLayout(text: timestamp, numberOfLines: 2, viewReuseId: "timestamp", config: labelConfig)

let verticalLabelStack = StackLayout(
axis: .vertical,
spacing: 2,
alignment: Alignment(vertical: .center, horizontal: .leading),
sublayouts: [nameAndConnectionDegree, headline, timestamp]
)

let profileImage = SizeLayout<UIImageView>(
size: CGSize(width: 50, height: 50),
viewReuseId: "profileImage",
config: { imageView in
imageView.image = UIImage(named: profileImageName)
}
)

super.init(
axis: .horizontal,
spacing: 4,
sublayouts: [
profileImage,
verticalLabelStack
]
)
}
}

本文标题:LayoutKit 简单分析

文章作者:史彦超

发布时间:2018年09月06日 - 22:09

最后更新:2021年07月20日 - 16:07

原始链接:https://doingself.github.io/2018/09/06/LayoutKit-%E7%AE%80%E5%8D%95%E5%88%86%E6%9E%90/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

Donate comment here