Skip to content

Commit 45018b2

Browse files
author
mac
committed
Fix the incorrect height issue with CocoaList Cell, Section, Header, and Footer
1 parent 264cb59 commit 45018b2

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

Sources/SwiftUIX/Intramodular/List/_PlatformTableHeaderFooterView.swift

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ class _PlatformTableHeaderFooterView<SectionModel: Identifiable, Content: View>:
1212
var item: SectionModel!
1313
var makeContent: ((SectionModel) -> Content)!
1414

15-
private var contentHostingController: UIViewController!
16-
17-
var rootView: some View {
18-
self.makeContent(item).id(item.id)
19-
}
15+
var contentHostingController: UIHostingController<RootView>!
2016

2117
public override init(reuseIdentifier: String?) {
2218
super.init(reuseIdentifier: reuseIdentifier)
@@ -34,7 +30,7 @@ class _PlatformTableHeaderFooterView<SectionModel: Identifiable, Content: View>:
3430
contentView.bounds.origin = .zero
3531
layoutMargins = .zero
3632

37-
contentHostingController = UIHostingController(rootView: rootView)
33+
contentHostingController = UIHostingController(rootView: RootView(base: self))
3834
contentHostingController.view.backgroundColor = .clear
3935
contentHostingController.view.translatesAutoresizingMaskIntoConstraints = false
4036

@@ -50,7 +46,24 @@ class _PlatformTableHeaderFooterView<SectionModel: Identifiable, Content: View>:
5046
contentHostingController.view.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
5147
])
5248
} else {
53-
(contentHostingController as? UIHostingController)?.rootView = rootView
49+
contentHostingController.rootView = RootView(base: self)
50+
}
51+
}
52+
}
53+
54+
extension _PlatformTableHeaderFooterView {
55+
struct RootView: View {
56+
private let id: AnyHashable
57+
private let content: Content
58+
59+
init(base: _PlatformTableHeaderFooterView<SectionModel, Content>) {
60+
self.content = base.makeContent(base.item)
61+
self.id = base.item.id
62+
}
63+
64+
var body: some View {
65+
content
66+
.id(id)
5467
}
5568
}
5669
}

Sources/SwiftUIX/Intramodular/List/_PlatformTableViewController.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ public class _PlatformTableViewController<SectionModel: Identifiable, ItemType:
202202
prototypeSectionHeader.update()
203203

204204
let height = prototypeSectionHeader
205-
.contentView
206-
.systemLayoutSizeFitting(UIView.layoutFittingExpandedSize)
205+
.contentHostingController
206+
.sizeThatFits(in: CGSize(width: tableView.bounds.width, height: UIView.layoutFittingExpandedSize.height))
207207
.height
208208

209209
_sectionHeaderContentHeightCache[model.id] = height
@@ -256,8 +256,8 @@ public class _PlatformTableViewController<SectionModel: Identifiable, ItemType:
256256
prototypeSectionFooter.update()
257257

258258
let height = prototypeSectionFooter
259-
.contentView
260-
.systemLayoutSizeFitting(UIView.layoutFittingExpandedSize)
259+
.contentHostingController
260+
.sizeThatFits(in: CGSize(width: tableView.bounds.width, height: UIView.layoutFittingExpandedSize.height))
261261
.height
262262

263263
_sectionFooterContentHeightCache[model.id] = height
@@ -307,7 +307,7 @@ public class _PlatformTableViewController<SectionModel: Identifiable, ItemType:
307307

308308
let height = prototypeCell
309309
.contentHostingController
310-
.sizeThatFits(in: UIView.layoutFittingExpandedSize)
310+
.sizeThatFits(in: CGSize(width: tableView.bounds.width, height: UIView.layoutFittingExpandedSize.height))
311311
.height
312312

313313
_rowContentHeightCache[item.id] = height

0 commit comments

Comments
 (0)