简体   繁体   English

Swift iOS - UITableView 自定义单元格未出现?

[英]Swift iOS - UITableView Custom Cell not appearing?

I'm trying to populate my tableView with a custom cell - seems pretty straight forward, but for some reason when I run the following code, the custom cell doesn't appear?我正在尝试用自定义单元格填充我的 tableView - 看起来很简单,但由于某种原因,当我运行以下代码时,自定义单元格没有出现? I feel like I'm missing something super obvious.我觉得我错过了一些非常明显的东西。

Custom cell name is: OffersTableViewCell自定义单元格名称为: OffersTableViewCell

DashboardViewController仪表板视图控制器

class DashboardViewController: UIViewController, UITableViewDelegate, UITableViewDataSource  {

    
 @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.tableView.dataSource = self
        self.tableView.delegate = self
        
        self.registerTableViewCells()
    }

    func tableView(_ tableView: UITableView,
                   numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(_ tableView: UITableView,
                   cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let cell = tableView.dequeueReusableCell(withIdentifier: "OffersTableViewCell") as? OffersTableViewCell {
            return cell
        }
        
        return UITableViewCell()
    }
    
    private func registerTableViewCells() {
        let textFieldCell = UINib(nibName: "OffersTableViewCell",
                                  bundle: nil)
        self.tableView.register(textFieldCell,
                                forCellReuseIdentifier: "OffersTableViewCell")
    }
    
 

}

OffersTableViewCell OffersTableViewCell

import UIKit

class OffersTableViewCell: UITableViewCell {

    @IBOutlet weak var offerTitle: UILabel!
    
    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
    }
    
}

I think you should be able to resolve the issue by using the code below.我认为您应该能够使用下面的代码解决问题。 I would suggest we remove UITableViewCell().我建议我们删除 UITableViewCell()。


    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.tableView.dataSource = self
        self.tableView.delegate = self
        
        self.registerTableViewCells()
        self.tableView.reloadData()
    }

    func tableView(_ tableView: UITableView,
                   cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "OffersTableViewCell", for: indexPath) as! OffersTableViewCell 
        return cell
        //put any other exception conditions to display UITableViewCell() if necessary
    }

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

Your code works for me.你的代码对我有用。 Try to go through these steps:通过以下步骤尝试 go:

  1. Check if .xib file named as OffersTableViewCell.xib检查是否名为OffersTableViewCell.xib.xib文件在此处输入图像描述
  2. Check if File's Owner class is OffersTableViewCell检查文件所有者 class 是否为OffersTableViewCell 在此处输入图像描述
  3. Make sure you deleted default view in .xib and created UITableViewCell确保您删除了.xib中的默认视图并创建了UITableViewCell
  4. Check if class of cell is OffersTableViewCell检查单元格的 class 是否为OffersTableViewCell 在此处输入图像描述
  5. Check if cell identifier is OffersTableViewCell检查单元格标识符是否为OffersTableViewCell 在此处输入图像描述
  6. Check if class of view controller in storyboard is DashboardViewController检查storyboard中视图controller的class是否为DashboardViewController
  7. Make sure you open DashboardViewController in app确保在应用程序中打开DashboardViewController

Make sure you have the Cell named - OffersTableViewCell.swift class, and OffersTableViewCell.xib that is having UI for that cell.确保您有名为 OffersTableViewCell.swift class 的单元格,以及具有该单元格 UI 的 OffersTableViewCell.xib。

override func viewDidLoad() {
    super.viewDidLoad()
    
    self.registerTableViewCells()

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

func tableView(_ tableView: UITableView,
               cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "OffersTableViewCell", for: indexPath) as? OffersTableViewCell else { return UITableViewCell() }
     
    cell.offerTitle.text = "blabla.."

    return cell
}

Please register cell before you assign the tableView delegate & datasource.请在分配 tableView 委托和数据源之前注册单元格。 Or just call reloadData after you register cell Nibs.或者在注册单元格 Nib 后调用 reloadData。

Also, make sure you are loading safe data otherwise return default TableViewCell please.另外,请确保您正在加载安全数据,否则请返回默认的 TableViewCell。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM