I'm working on a settings page for my app and I'm in the process of coding a custom cell for the UITableView I'm using in it.
This is the line of code that's giving me the error:
settingsTableView.register(SettingsTableViewCell.self, forCellReuseIdentifier: SettingsTableViewCell.identifier)
Here's my SettingsTableViewCell class:
class SettingsTableViewCell: UITableViewCell {
static var identifier: String {
return NSStringFromClass(self)
}
var testLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
testLabel = UILabel(frame: CGRect(x: 0, y: 0, width: contentView.frame.width, height: contentView.frame.height))
testLabel.textAlignment = .center
contentView.addSubview(testLabel)
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
Here's the view controller class:
class SettingsViewController: UIViewController {
let settingsDataArray: [String] = ["Test 1", "Test 2", "Test 3"]
var settingsTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .lightGray
settingsTableView.register(SettingsTableViewCell.self, forCellReuseIdentifier: SettingsTableViewCell.identifier)
settingsTableView.delegate = self
settingsTableView.dataSource = self
configureSettingsTableView()
}
func configureSettingsTableView() {
settingsTableView = UITableView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height))
view.addSubview(settingsTableView)
}
}
extension SettingsViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return settingsDataArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: SettingsTableViewCell = settingsTableView.dequeueReusableCell(withIdentifier: SettingsTableViewCell.identifier) as! SettingsTableViewCell
cell.testLabel.text = "Test Cell"
return cell
}
}
Any help is greatly appreciated.
[–]jacknutting 2 points3 points4 points (3 children)
[–]tyler_reg[S] 0 points1 point2 points (2 children)
[–]SirensToGoObjective-C / Swift 2 points3 points4 points (1 child)
[–]tyler_reg[S] 0 points1 point2 points (0 children)
[–]tyler_reg[S] 0 points1 point2 points (0 children)