How to delete the space between UITableViewCell in plain style UITableView on visionOS?

63 views Asked by At

enter image description here

The picture above is the effect of the following sample code after running on visionOS. Below is the full sample code:

final class TestViewController: UIViewController {
    private lazy var tableView: UITableView = {
        let _view = UITableView(frame: .zero, style: .plain)
        _view.translatesAutoresizingMaskIntoConstraints = false
        _view.backgroundColor = .red
        _view.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        _view.delegate = self
        _view.dataSource = self
        return _view
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(tableView)
        
        NSLayoutConstraint.activate([
            tableView.topAnchor.constraint(equalTo: view.topAnchor),
            tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            tableView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
            tableView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor)
        ])
    }
}

// MARK: - UITableViewDelegate

extension TestViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 24.3
    }
}

// MARK: - UITableViewDataSource

extension TestViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 50
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        cell.textLabel?.text = "\(indexPath)"
        cell.backgroundColor = .blue
        return cell
    }
}

As can be seen from the screenshot, there is a certain spacing between Cells (blue), and the TableView (red) is not filled on the left and right.

I searched for relevant information but found nothing that could help me. Does anyone know how to remove the spacing between Cells? Looking forward to getting help, my sincerest gratitude!

1

There are 1 answers

3
Hal Mueller On

[edit: now I have]. I haven’t tested this.

Since we’re in UIKit land (even though we’re on visionOS), the simplest way I can think of to achieve what you’ve observed would be if visionOS has changed the UITableView default .separatorColor to transparent.

Try tableView.separatorColor = .yellow to see if that’s what is going on. Then tableView.separatorColor = .blue to make it look like the cells are continuous with no space between.