将SwiftUI视图添加到UITableViewCell的contentView

12

我目前正在尝试在UIViewControllerRepresentable中实现UITableViewController,其中单元格的内容再次是SwiftUI视图。我无法使用SwiftUI列表,因为我想稍后添加UISearchController。
因为我希望能够将自定义的SwiftUI视图放置在每个单元格的内容中,所以我没有其他选择,必须在单元格内使用SwiftUI视图。
我的当前代码如下,但不起作用:

class SearchableListCell: UITableViewCell {
    let contentController: UIViewController

    init(withContent content: UIViewController, reuseIdentifier: String) {
        self.contentController = content

        super.init(style: .default, reuseIdentifier: reuseIdentifier)

        self.addSubview(self.contentController.view)
        // Tried also
        // self.contentView.addSubview(self.contentController.view)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

struct SearchableList: UIViewControllerRepresentable {
    let data: [String]

    var viewBuilder: (String) -> ContentView

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    func makeUIViewController(context: Context) -> UITableViewController {
        return context.coordinator.tableViewController
    }

    func updateUIViewController(_ tableViewController: UITableViewController, context: Context) {
    }

    class Coordinator: NSObject, UITableViewDataSource, UITableViewDelegate {
        var parent: SearchableList

        let tableViewController = UITableViewController()

        init(_ searchableList: SearchableList) {
            self.parent = searchableList

            super.init()

            tableViewController.tableView.dataSource = self
            tableViewController.tableView.delegate = self
        }

        func numberOfSections(in tableView: UITableView) -> Int {
            return 1
        }

        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return parent.data.count
        }

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let string = self.parent.data[indexPath.row]

            let view = parent.viewBuilder(string)

            let hostingController = UIHostingController(rootView: view)

            let cell = SearchableListCell(withContent: hostingController, reuseIdentifier: "cell")

            // Tried it with and without this line:
            tableViewController.addChild(hostingController)

            return cell
        }
    }
}

当我运行这个程序时,例如使用这个预览设置:

#if DEBUG
struct SearchableList_Previews: PreviewProvider {
    static var previews: some View {
        NavigationView {
            SearchableList(data: ["Berlin", "Dresden", "Leipzig", "Hamburg"]) { string in
                NavigationLink(destination: Text(string)) { Text(string) }
            }
            .navigationBarTitle("Cities")
        }
    }
}
#endif

我只看到一个有4个明显空白单元格的TableView。但在视图层次结构调试器中,我可以看到每个单元格确实都有导航链接和文本作为子视图,只是不可见。因此,我认为这与将UIHostingController添加为UITableViewController的子项有关,但我不知道应该在哪里添加它。


现在有办法解决这个问题吗?


这是在UIKit中通常嵌入子视图控制器的方式吗? - Fabian
1
@Fabian,你可以在这里看一下。 - Josef Zoller
务实问题 - 您是否绝对需要将单元格作为SwiftUI视图?我有一个工作的UITableViewUITableViewRepresentable内部工作,因为需要一个UINavigationController子类。如果您在SwiftUI应用程序中的UIKit表格中使用SwiftUI唯一可以获得的好处是...向前兼容性?PreviewProvider?那么,请记住,UIViewControllerRepresentable被认为只是SwiftUI视图。 - user7014451
我刚在这里发布了一个问题:https://stackoverflow.com/questions/57676251/how-can-i-have-a-uibutton-uiviewcontrollerrepresentable-trigger-an-action-in-m 我的“生产”需求是创建一个可编辑的数组 - 就像您一样 - 带有添加按钮(以及编辑/完成),并浏览任何单元格进入详细视图。目前我遇到了我发布的问题。我的列表问题?将其嵌入NavigationView会限制外观。我不希望解决我的问题...但我正在探索的一个选项涉及使用SwiftUI按钮替换添加按钮.... - user7014451
这可能就是你需要的。如果我认为我能帮忙,我会发布我找到的内容。 - user7014451
显示剩余3条评论
3个回答

4

我在尝试做同样的事情时发现了这个方法,对我有效。

对我来说,这需要进行子类化并隐藏导航栏。

import UIKit
import SwiftUI
/// SwiftUI UIHostingController adds a navbar for some reason so we must disable it
class ControlledNavigationHostingController<Content>: UIHostingController<AnyView> where Content: View {

    public init(rootView: Content) {
        super.init(rootView: AnyView(rootView.navigationBarHidden(true)))
    }

    @objc dynamic required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.isNavigationBarHidden = true
    }
}

这是主要要使用的对象

/// This UITableViewCell  wrapper allows you to have a SwiftUI View in your UITableView
class HostingTableViewCell<Content: View>: UITableViewCell {
    /// This holds the SwiftUI View being displayed in this UITableViewCell wrapper
    private weak var swiftUIContainer: ControlledNavigationHostingController<Content>?
    /// Put the SwiftUI View into the contentView of the UITableViewCell, or recycle an exisiting instance and add the new SwiftUIView
    ///
    /// - Parameter view: The SwiftUI View to be used as a UITableViewCell
    /// - Parameter parent: The nearest UIViewController to be parent of the UIHostController displaying the SwiftUI View
    /// - Warning: May be unpredictable on the Simulator
    func host(_ view: Content, parent: UIViewController) {
        if let container = swiftUIContainer {
            // Recycle this view
            container.rootView = AnyView(view)
            container.view.layoutIfNeeded()

        } else {
            // Create a new UIHostController to display a SwiftUI View
            let swiftUICellViewController = ControlledNavigationHostingController(rootView: view)
            swiftUIContainer = swiftUICellViewController

            // Setup the View as the contentView of the UITableViewCell
            swiftUICellViewController.view.backgroundColor = .clear
            // Add the View to the hierarchy to be displayed
            parent.addChild(swiftUICellViewController)
            contentView.addSubview(swiftUICellViewController.view)
            swiftUICellViewController.view.translatesAutoresizingMaskIntoConstraints = false
            if let view = swiftUICellViewController.view {
                contentView.addConstraint(NSLayoutConstraint(item: view, attribute: .leading, relatedBy: .equal, toItem: contentView, attribute: .leading, multiplier: 1.0, constant: 0.0))
                contentView.addConstraint(NSLayoutConstraint(item: view, attribute: .trailing, relatedBy: .equal, toItem: contentView, attribute: .trailing, multiplier: 1.0, constant: 0.0))
                contentView.addConstraint(NSLayoutConstraint(item: view, attribute: .top, relatedBy: .equal, toItem: contentView, attribute: .top, multiplier: 1.0, constant: 0.0))
                contentView.addConstraint(NSLayoutConstraint(item: view, attribute: .bottom, relatedBy: .equal, toItem: contentView, attribute: .bottom, multiplier: 1.0, constant: 0.0))
            }
        
            swiftUICellViewController.didMove(toParent: parent)
            swiftUICellViewController.view.layoutIfNeeded()
        }
    }
}

并这样注册:

tableView.register(HostingTableViewCell<YourSwiftUIView>.self, forCellReuseIdentifier: "WhateverIDYouWant")

然后像这样使用:

guard let cell = tableView.dequeueReusableCell(withIdentifier: "YourCellID", for: indexPath) as? HostingTableViewCell<SomeSwiftUIView> else {
    print("Error: Could Not Dequeue HostingTableViewCell<SomeSwiftUIView>")
    return UITableViewCell()
}
cell.host(SomeSwiftUIView(), parent: self)

3
为解决单元格可见性问题,请将UIHostingController的translatesAutoresizingMaskIntoConstraints属性更改为false,然后将其视图框架设置为单元格contentView边界或使用NSLayoutConstraint,具体请参考以下内容。
class SearchableListCell: UITableViewCell {
    let contentController: UIViewController

    init(withContent content: UIViewController, reuseIdentifier: String) {
        self.contentController = content

        super.init(style: .default, reuseIdentifier: reuseIdentifier)


        contentController.view.translatesAutoresizingMaskIntoConstraints = false
        contentController.view.frame = self.contentView.bounds

        self.contentView.addSubview(self.contentController.view)

    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

0

iOS 16及以上版本

UIHostingConfiguration 适用于托管SwiftUI视图层次结构的内容配置。 https://developer.apple.com/documentation/SwiftUI/UIHostingConfiguration

class SomeCell: UITableViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    func set(someData: SomeData) {
        self.contentConfiguration = UIHostingConfiguration {
            SomeView(someData: someData)
        }
        .margins(.all, 0)
    }
    
}

默认情况下,以这种方式添加视图会在其周围添加填充。为了去除它,我们使用 .margins(.all, 0)。感谢 在单元格中使用 UIHostingConfiguration 时如何去除填充?

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接