You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift4中UIViewController上TableView及Cell不显示问题求助

Troubleshooting Empty TableView with ProgressView in Swift 4

Hey there! Let's work through your empty TableView issue step by step—no error messages can make this feel a bit tricky, but we can check all the common culprits one by one to get those ProgressViews showing up.

1. Double-Check Data Source & Delegate Setup

First, make sure your UIViewController is properly connected to the TableView's data source and delegate:

  • Confirm your class declaration includes the required protocols:
    class YourProgressVC: UIViewController, UITableViewDataSource, UITableViewDelegate
    
  • In viewDidLoad(), explicitly set the data source and delegate (even if you connected them in Storyboard, this can fix hidden connection issues):
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.dataSource = self
        tableView.delegate = self
    }
    
  • Verify your TableView's IBOutlet is correctly linked (check for broken connections in the Connections Inspector in Xcode).

2. Ensure TableView Has Valid Constraints

An empty screen often means the TableView isn't being sized correctly:

  • Use Auto Layout to pin the TableView to all four edges of its parent view (top, bottom, leading, trailing). No gaps, no conflicting constraints.
  • You can check the TableView's actual size by opening the Debug View Hierarchy (Xcode's debug bar icon that looks like a stack of squares) to see if it's being compressed to 0 height/width.

3. Fix Cell Registration & Reuse

If your cells aren't loading, it's usually a problem with registration or reuse identifiers:

  • Storyboard cells: Go to your TableView Cell in Storyboard, set a clear reuse identifier (e.g., "ProgressCell") in the Attributes Inspector. Then use that exact identifier in cellForRowAt:
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ProgressCell", for: indexPath)
        // Configure ProgressView here
        return cell
    }
    
  • Code-based cells: Register the cell class in viewDidLoad() before trying to dequeue it:
    tableView.register(ProgressCell.self, forCellReuseIdentifier: "ProgressCell")
    
  • Critical: Make sure numberOfRowsInSection returns a value greater than 0! For testing, hardcode a number like return 5 to rule out empty data sources:
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5 // Test with fixed count first
    }
    

4. Verify ProgressView Layout in Cells

Even if cells load, the ProgressView might be invisible due to bad layout:

  • If using a custom cell, add the ProgressView to the cell's contentView (not directly to the cell) and set proper Auto Layout constraints:
    class ProgressCell: UITableViewCell {
        let progressView = UIProgressView(style: .default)
        
        override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
            super.init(style: style, reuseIdentifier: reuseIdentifier)
            setupProgressView()
        }
        
        required init?(coder: NSCoder) {
            super.init(coder: coder)
            setupProgressView()
        }
        
        private func setupProgressView() {
            progressView.translatesAutoresizingMaskIntoConstraints = false
            contentView.addSubview(progressView)
            
            NSLayoutConstraint.activate([
                progressView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 16),
                progressView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -16),
                progressView.centerYAnchor.constraint(equalTo: contentView.centerYAnchor),
                progressView.heightAnchor.constraint(equalToConstant: 8)
            ])
        }
    }
    
  • For Storyboard cells, drag a ProgressView into the cell and pin it with constraints to ensure it's visible.

5. Check Tab Bar Controller Integration

Make sure your UIViewController is properly added to the Tab Bar Controller:

  • Confirm it's included in the Tab Bar Controller's viewControllers array (check in Storyboard or in code if you're setting it programmatically).
  • You can add a print statement in viewDidAppear() to verify the parent controller:
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        print("Parent controller: \(self.parent)") // Should show your TabBarController
    }
    

Quick Test Tip

Start with the simplest possible implementation: hardcode numberOfRowsInSection to 3, use a default UITableViewCell, and add a ProgressView directly in cellForRowAt to see if anything shows up. If that works, you can gradually add back your custom cell and Firebase logic.

Once the TableView is working, integrating Firebase's countdown value will involve fetching the speed, creating a timer to update the ProgressView's progress over time, and listening for real-time updates if you need syncing.

内容的提问来源于stack exchange,提问作者Darragh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:58:52