I created a custom table view cell and am trying to load cells with just test strings shown below. The tableview is created in code and is shown on device but the cells aren't being loaded. So I think (But I could be wrong) that there is an issue in my custom cell class or in my extensions code.
Custom TableView Cell
import UIKit
class TableViewCell: UITableViewCell {
var titleView = UILabel()
var descriptionView = UILabel()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
addSubview(titleView)
addSubview(descriptionView)
configureTitleView()
configureDescriptionView()
setTitleConstraints()
setDescriptionConstraints()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func set(title: String, description: String){
titleView.text = title
descriptionView.text = description
}
func configureTitleView() {
titleView.numberOfLines = 0
titleView.adjustsFontSizeToFitWidth = true
}
func configureDescriptionView(){
descriptionView.numberOfLines = 0
descriptionView.adjustsFontSizeToFitWidth = true
}
func setTitleConstraints(){
titleView.translatesAutoresizingMaskIntoConstraints = false
titleView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
titleView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 12).isActive = true
titleView.heightAnchor.constraint(equalToConstant: 80).isActive = true
titleView.widthAnchor.constraint(equalToConstant: frame.width / 2 ).isActive = true
}
func setDescriptionConstraints(){
descriptionView.translatesAutoresizingMaskIntoConstraints = false
descriptionView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
descriptionView.leadingAnchor.constraint(equalTo: titleView.trailingAnchor, constant: 12).isActive = true
descriptionView.heightAnchor.constraint(equalToConstant: 80).isActive = true
descriptionView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -12).isActive = true
}
}
Method called in ViewDidLoad - This Works
// MARK: - Base Table View Set UP
private func TableViewSetUp(){
tableView?.delegate = self
tableView?.dataSource = self
tableView = UITableView(frame: .zero, style: .plain)
tableView?.backgroundColor = .brown // Testing Only
tableView?.rowHeight = 100
tableView?.register(TableViewCell.self, forCellReuseIdentifier: "Cell")
guard let myTableView = tableView else { return }
view.addSubview(myTableView)
// Constraints
myTableView.translatesAutoresizingMaskIntoConstraints = false
myTableView.topAnchor.constraint(equalTo: (collectionView?.bottomAnchor)!).isActive = true
myTableView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
myTableView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
myTableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}
How Im trying to add cell info
extension LogViewController: UITableViewDelegate{
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
}
}
extension LogViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("IN CELLFORROWAT")
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! TableViewCell
cell.set(title: "BOOO", description: "aaa")
cell.backgroundColor = .blue
return cell
}
}
tableView?.delegate = self tableView?.dataSource = self
These should be after you create your TableView in the next like
so instead of
private func TableViewSetUp(){
tableView?.delegate = self
tableView?.dataSource = self
tableView = UITableView(frame: .zero, style: .plain)
tableView?.backgroundColor = .brown // Testing Only
tableView?.rowHeight = 100
tableView?.register(TableViewCell.self, forCellReuseIdentifier: "Cell")
use this
private func TableViewSetUp(){
tableView = UITableView(frame: .zero, style: .plain)
tableView?.delegate = self
tableView?.dataSource = self
tableView?.backgroundColor = .brown // Testing Only
tableView?.rowHeight = 100
tableView?.register(TableViewCell.self, forCellReuseIdentifier: "Cell")
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.