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

Xib中嵌套UITableViewCell加载及显示异常问题求助

Troubleshooting Nested UITableView Cell Not Displaying Issue

Hey there, let's work through this nested UITableView problem together. Based on your description, you've got a main UITableView in your ViewController, each cell containing a child UITableView that's supposed to show DepartureInsideTableViewCell—but those child cells aren't appearing between your date views. Let's break down the fixes step by step:

First, Fix the Main TableView Cell Registration

Looking at your initial code, you registered the default UITableViewCell instead of your custom XIB-based cell. That's a common misstep. Update your registration code in the ViewController:

tableDetails.delegate = self 
tableDetails.dataSource = self 
// Replace the default cell registration with your custom XIB
tableDetails.register(UINib(nibName: "DepartureDetailTableViewCell", bundle: nil), forCellReuseIdentifier: "DepartureDetailTableViewCell")

Manage the Child TableView Inside Your Main Cell

The child UITableView belongs to DepartureDetailTableViewCell, so we need to configure it directly within that cell class, not the ViewController. Here's how to set it up:

  1. Configure the Child TableView in DepartureDetailTableViewCell
    Add this code to your custom cell class to handle registration, data source, and delegate:
class DepartureDetailTableViewCell: UITableViewCell {
    @IBOutlet weak var insideTableView: UITableView!
    
    // Store data for the child table view
    var childData: [YourChildDataModel] = [] {
        didSet {
            insideTableView.reloadData()
            insideTableView.layoutIfNeeded() // Trigger layout update when data changes
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        // Register the child cell's XIB
        insideTableView.register(UINib(nibName: "DepartureInsideTableViewCell", bundle: nil), forCellReuseIdentifier: "DepartureInsideTableViewCell")
        
        insideTableView.delegate = self
        insideTableView.dataSource = self
        
        // Enable dynamic row height for the child table view
        insideTableView.estimatedRowHeight = 44
        insideTableView.rowHeight = UITableView.automaticDimension
        
        // Hide empty separator lines
        insideTableView.tableFooterView = UIView()
    }
}

// Extend to handle child table view data source/delegate
extension DepartureDetailTableViewCell: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // Return the count of your child data (critical—if this is 0, no cells show!)
        return childData.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "DepartureInsideTableViewCell", for: indexPath) as! DepartureInsideTableViewCell
        
        // Configure the child cell with your data
        let data = childData[indexPath.row]
        cell.configure(with: data) // Replace with your actual configuration method
        
        return cell
    }
}
  1. Pass Child Data to the Main Cell from the ViewController
    In your ViewController's cellForRowAt method, pass the necessary data to the main cell so the child table view has content to display:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "DepartureDetailTableViewCell", for: indexPath) as! DepartureDetailTableViewCell
    
    // Assume your main data array contains child data for each row
    let mainRowData = yourMainDataArray[indexPath.row]
    cell.childData = mainRowData.childItems // Pass the child data here
    
    return cell
}

Fix Layout & Height Issues

If the child cells still don't show up, check these layout details:

  • Child TableView Constraints: Ensure the child UITableView in DepartureDetailTableViewCell has top, bottom, leading, and trailing constraints pinned to the cell's contentView. This ensures it takes up the correct space and expands with its content.
  • Child Cell Constraints: Make sure DepartureInsideTableViewCell has proper AutoLayout constraints for its subviews (labels, etc.) so the cell's height can be calculated automatically.
  • Main TableView Dynamic Height: Enable automatic row height for the main table view to let it adjust based on the child table view's content:
    // Add this to your ViewController's viewDidLoad
    tableDetails.estimatedRowHeight = 200 // Set a reasonable estimate
    tableDetails.rowHeight = UITableView.automaticDimension
    

Quick Checks to Rule Out Simple Issues

  • Verify that childData in DepartureDetailTableViewCell isn't empty (if numberOfRowsInSection returns 0, no cells will render).
  • Double-check that your XIB filenames match exactly what you're using in UINib(nibName:...) (case-sensitive!).
  • Ensure the child table view's outlet is properly connected in the XIB.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 06:53:32