UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Wrapped UITableViewController sometimes doesn't select row correctly

Forums > SwiftUI

Hey all, I'm embedding a UITableViewController in a swiftUI app, so I've made a class that conforms to UIViewControllerRepresentable. When I put the table into editing mode, a row will occasionally be completely unselectable in the simulator. Can someone provide any tips on how to debug / figure out what might be going wrong?

This is a screen capture of what I'm seeing: Screen capture of buggy behavior

This is the wrapped table view

import SwiftUI

final class CommentsTableViewWrapper: UIViewControllerRepresentable {
    // MARK: Properties and initializer
    let post: String
    var comments: [String]
    @Binding var isEditing: Bool

    var vc: UITableViewController?

    init(_ comments: [String], post: String, isEditing: Binding<Bool>) {
        self.comments = comments
        self.post = post
        self._isEditing = isEditing
    }

    // MARK: UIViewControllerRepresentable
    typealias UIViewControllerType = UITableViewController

    func makeUIViewController(context: Context) -> UITableViewController {
        let tvc = UITableViewController()
        tvc.tableView.delegate = context.coordinator
        tvc.tableView.dataSource = context.coordinator

        tvc.tableView.rowHeight = UITableView.automaticDimension
        tvc.tableView.estimatedRowHeight = UITableView.automaticDimension
        tvc.tableView.separatorStyle = .none
        tvc.tableView.allowsSelection = false
        tvc.tableView.allowsSelectionDuringEditing = true
        tvc.tableView.allowsMultipleSelectionDuringEditing = true

        self.vc = tvc
        return tvc
    }

    func updateUIViewController(_ uiViewController: UITableViewController, context: Context) {
        uiViewController.tableView.reloadData()
        uiViewController.setEditing(isEditing, animated: true)
    }

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

    // MARK: - Coordinator
    class Coordinator: NSObject, UITableViewDataSource, UITableViewDelegate {

        // MARK: Properties and initializer
        private let parent: CommentsTableViewWrapper

        init(_ parent: CommentsTableViewWrapper) {
            self.parent = parent
        }

        // MARK: UITableViewDelegate and DataSource methods
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return parent.comments.count
        }

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            print(indexPath)

            return createCommentCell(indexPath, tableView)
        }

        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            UITableView.automaticDimension
        }

        var cellHeights = [IndexPath: CGFloat]()

        func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
            cellHeights[indexPath] = cell.frame.size.height
        }

        func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
            return cellHeights[indexPath] ?? UITableView.automaticDimension
        }

        // MARK: - Private helpers
        fileprivate func createCommentCell(_ indexPath: IndexPath, _ tableView: UITableView) -> UITableViewCell {
            let comment = parent.comments[indexPath.row]
            let cell = tableView.dequeueReusableCell(withIdentifier: "DebugCell") ?? UITableViewCell(style: .subtitle, reuseIdentifier: "DebugCell")

            cell.textLabel!.numberOfLines = 0
            cell.textLabel!.lineBreakMode = .byCharWrapping
            cell.textLabel!.text = comment
            return cell
        }
    }
}

This is the swiftui view that embeds the wrapped tableview

struct ContentView: View {
    @State var isEditing = false

    var body: some View {
        NavigationView {
        CommentsTableViewWrapper(["Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1", "Test2", "Test3","Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1", "Test2", "Test3","Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1", "Test2", "Test3","Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1", "Test2", "Test3","Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1", "Test2", "Test3"], post: "This is the post text!", isEditing: self.$isEditing)
            .navigationBarTitle("Nav title", displayMode: .inline)
            .navigationBarItems(trailing: Button("Edit") {
                self.isEditing = true
            })
        }
    }
}

2      

@ciniglio when I ran this in my editor on an ipod touch simulator it worked fantastically! Are you still experiencing an issue?

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.