我已将UIKit UICollectionView
集成到SwiftUI View
中:
struct MySwiftUIView: View {
MyCollectionView()
}
struct MyCollectionView: UIViewRepresentable {
private var coordinator: MyCollectionViewCoordinator
func makeUIView(context: Context) -> UICollectionView {
let collectionView = UICollectionView(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
collectionView.dataSource = coordinator
collectionView.delegate = coordinator
return collectionView
}
func updateUIView(_ uiView: UICollectionView, context: Context) {}
}
final class MyCollectionViewCoordinator: NSObject, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// Here?
}
}
现在,我想知道是否可以在View
函数中使用SwiftUI UICollectionViewCell
代替UIKit cellForItemAt()
。
谢谢您的帮助。