Reputation: 7
So im trying build app with no storyboard. I have made custom tableview cell class and trying display a title and image from an array. The code runs and even creates cells that number same as array but cells are blank. If anyone could have a look over this that would be great. Thanks
This is my custom cell
class CoffeeCell: UITableViewCell {
let cellImage = UIImageView()
let cellTitalBackground = UIView()
let cellTital = UILabel()
override func awakeFromNib() {
super.awakeFromNib()
self.backgroundColor = #colorLiteral(red: 0.9324248433, green: 0.9268818498, blue: 0.9366856217, alpha: 1)
self.layer.cornerRadius = 20
self.addSubview(cellImage)
self.addSubview(cellTitalBackground)
self.addSubview(cellTital)
setUpCellImage()
setUpCellTitalBackground()
setUpCellTital()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func setUpCell(Coffee: Coffee) {
self.cellImage.image = UIImage(named: Coffee.image)
self.cellTital.text = Coffee.tital
and here is the view controller code for table view
import UIKit
class CoffeeVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
let logoImage = UIImage(named: "DIYCoffeeLogoDark")
let logoImageView = UIImageView()
let pageHeader = UILabel()
let coffeeTableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
coffeeTableView.dataSource = self
coffeeTableView.delegate = self
view.addSubview(pageHeader)
view.addSubview(coffeeTableView)
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return DataService.instance.coffees.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = coffeeTableView.dequeueReusableCell(withIdentifier: "CoffeeCell", for: indexPath) as? CoffeeCell {
let coffee = DataService.instance.getCoffee()[indexPath.row]
cell.setUpCell(Coffee: coffee)
print ("it works")
return cell
} else {
return CoffeeCell()
}
Hope that all makes sense
here is added all the code for my cell class
import UIKit
class CoffeeCell: UITableViewCell {
let cellImage = UIImageView()
let cellTitalBackground = UIView()
let cellTital = UILabel()
override func awakeFromNib() {
super.awakeFromNib()
self.backgroundColor = #colorLiteral(red: 0.9324248433, green: 0.9268818498, blue: 0.9366856217, alpha: 1)
self.layer.cornerRadius = 20
self.addSubview(cellImage)
self.addSubview(cellTitalBackground)
self.addSubview(cellTital)
setUpCellImage()
setUpCellTitalBackground()
setUpCellTital()
layoutIfNeeded()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func setUpCell(Coffee: Coffee) {
self.cellImage.image = UIImage(named: Coffee.image)
self.cellTital.text = Coffee.tital
}
func setUpCellImage() {
cellImage.contentMode = .scaleAspectFill
cellImage.translatesAutoresizingMaskIntoConstraints = false
cellImage.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
cellImage.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
cellImage.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
cellImage.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
cellImage.clipsToBounds = true
}
func setUpCellTitalBackground() {
cellTitalBackground.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 0.7847549229)
cellTitalBackground.translatesAutoresizingMaskIntoConstraints = false
cellTitalBackground.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
cellTitalBackground.trailingAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
cellTitalBackground.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
cellTitalBackground.heightAnchor.constraint(equalToConstant: 40).isActive = true
cellTitalBackground.layer.cornerRadius = 20
cellTitalBackground.layer.maskedCorners = .layerMaxXMinYCorner
}
func setUpCellTital() {
cellTital.textColor = .black
cellTital.textAlignment = .center
cellTital.font = UIFont(name: "Futura", size: 30)
cellTital.adjustsFontSizeToFitWidth = true
cellTital.translatesAutoresizingMaskIntoConstraints = false
cellTital.leadingAnchor.constraint(equalTo: cellTitalBackground.leadingAnchor).isActive = true
cellTital.bottomAnchor.constraint(equalTo: cellTitalBackground.bottomAnchor).isActive = true
cellTital.trailingAnchor.constraint(equalTo: cellTitalBackground.trailingAnchor).isActive = true
cellTital.topAnchor.constraint(equalTo: cellTitalBackground.topAnchor).isActive = true
}
}
Upvotes: 0
Views: 85
Reputation: 7
thanks for all your input and help everyone
had another look and reason wasn't working in th custom cell I was overriding the awakeFromNib() function but because im not using storyboards I needed to use the init functions shown below.
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setUpView()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
sorry was a silly mistake thanks again.
Upvotes: 0
Reputation: 1152
You missed to create UITableViewCell instance when it's nil
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = coffeeTableView.dequeueReusableCell(withIdentifier: "CoffeeCell", for: indexPath) as? CoffeeCell
if cell == nil {
cell = CoffeeCell(style: .default, reuseIdentifier: "CoffeeCell")
}
cell?.setUpCell(Coffee: coffee)
return cell!
You use identifier to reuse cell, but for begining, the cell is nil, never created, so you have to create when cell is nil.
This is my custom cell and it's running well as expected.
Upvotes: 0
Reputation: 1225
Please update your code as follows. Looks like your image and title label frame is not updating when you assign the image and title. So, move your constraints and addsubView to setUpCell method.
override func awakeFromNib() {
super.awakeFromNib()
self.backgroundColor = #colorLiteral(red: 0.9324248433, green: 0.9268818498, blue: 0.9366856217, alpha: 1)
self.layer.cornerRadius = 20
}
func setUpCell(Coffee: Coffee) {
self.addSubview(cellImage)
self.addSubview(cellTitalBackground)
self.addSubview(cellTital)
setUpCellImage()
setUpCellTitalBackground()
setUpCellTital()
self.cellImage.image = UIImage(named: Coffee.image)
self.cellTital.text = Coffee.title
}
Output:-
Upvotes: 0
Reputation: 2368
You have to check constraints set up properly for image and label and see the result.
Upvotes: 0
Reputation: 5225
I believe you have already set the constranits right. You also need to set height of tableViewCells like this:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50 // whatever you want
}
Upvotes: 1