I want to set an UITableView in an UIViewController. The UITableView has UITableViewCells which has an UILabel.
However, UITableView.UILabels return nil.
Please see the following codes and images and share your thoughts.
Thank you in advance.
I am developing this with Swift5.
import UIKit
class condimentTableCell: UITableViewCell {
@IBOutlet weak var condimentName: UILabel!
}
class SandwichViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var condimentTable: UITableView!
@IBOutlet weak var breadSelector: UIPickerView!
fileprivate let items:[product] = products().getData()
fileprivate let cart:cart = Registry.instance.liveCart
var itemNo: Int = 0
let condiments = ["Lettuce", "Mayo", "Mustard", "Purple Onions", "Tomato"]
let breads = ["Rye", "Sourdough", "Squaw", "Wheat", "White"]
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "background.png")!)
self.breadSelector.dataSource = self
self.breadSelector.delegate = self
self.condimentTable.register(condimentTableCell.self, forCellReuseIdentifier: "condimentCell")
self.condimentTable.delegate = self
self.condimentTable.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return condiments.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:condimentTableCell = tableView.dequeueReusableCell(withIdentifier: "condimentCell", for: indexPath) as! condimentTableCell
if (cell.condimentName == nil) {
print("nil")
} else {
cell.condimentName.text = condiments[indexPath.row]
}
print("----------------------------------------------")
return cell
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return breads.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return breads[row]
}
}
Remove line
self.condimentTable.register(condimentTableCell.self, forCellReuseIdentifier: "condimentCell")
You are breaking the storyboard connection to the cell.
For detailed explanation see https://stackoverflow.com/a/44107114/669586
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.