简体   繁体   中英

Custom cell content doesn't show Swift

I want to create a custom table view. I have an UITableViewController:

class MainListView: UITableViewController{
var invoceList: [InvoceObject] = []
@IBOutlet var tbMain: UITableView!

  override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.register(CustomCell.self, forCellReuseIdentifier: "cell")
    tbMain.delegate = self
    self.tbMain.dataSource = self
    }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()        
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.invoceList.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell        
    cell.lblBuyerName?.text = self.invoceList[indexPath.row].BUYER_NAME
    cell.lblDate?.text = self.invoceList[indexPath.row].CREATED_AT
    cell.lblPriceGross?.text = self.invoceList[indexPath.row].PRICE + " " + self.invoceList[indexPath.row].CURRENCY

    cell.backgroundColor = UIColor(white: 1, alpha: 0.5)      
    return cell
    }
}

And my UITableViewCell class:

class CustomCell: UITableViewCell {
@IBOutlet weak var lblBuyerName: UILabel!
@IBOutlet weak var lblDate: UILabel!
@IBOutlet weak var lblPriceGross: 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
}
}

After build I get an empty cells. I have a number of rows like invoceList.count, but there is no labels and it looks as empty table. What I'm doing wrong?

Since you are using UITableViewController there is an implicit tableView property with connected data source and delegate. You should use that to avoid confusion.

And delete the line self.tableView.register... if you are using prototype cells.

If the cell's contents are defined in a separate xib file, you need to register that, not the class - if you only register the class, the actual labels are not being created because the class itself does not create them.

//Replacing CustomCellNibName with the name of your xib file
tableView.register(UINib.init(nibName: "CustomCellNibName", bundle: nil), forCellReuseIdentifier: "Cell")

If the cell is being created in the storyboard as a prototype cell, you do not need to register it, but just associate the class with your cell in the storyboard file (under the identity inspector tab, Custom Class), and fill in the name you want to use (in this case, "Cell") under the Attributes inspector field Identifier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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