У меня есть tableView
, назначенный моему ShopViewController
этому контроллеру, который называется ShopCellClass
для программного рисования каждой строки tableView
.ShopCellClass позвонил в мой shopClass, чтобы узнать, сколько предметов продается.
Проблема в моем shopClass. У меня есть UIColor для назначения на заднем плане ячейки, но последняя ячейка не имеет нужного цвета.
Я покажу вам код моих 3 файлов:
ShopViewController
:
class ShopViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(ShopCellClass.self, forCellReuseIdentifier: "CellShop")
tableView.rowHeight = 155
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return shop.data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:"CellShop") as! ShopCellClass
cell.drawTheCard(index: indexPath.row)
return cell
}
}
ShopCellClass
:
class ShopCellClass : UITableViewCell {
var widthOfCard : CGFloat = 0
var heightOfCard : CGFloat = 0
var cardIsDraw : Bool = false
let cardView = UIView()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.contentView.addSubview(cardView)
widthOfCard = UIScreen.main.bounds.size.width
heightOfCard = 155
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func drawTheCard(index : Int){
if(cardIsDraw == false){
drawCard(index: index)
cardIsDraw = true
}
}
private func drawCard(index: Int){
cardView.frame = CGRect(x : 0, y : 0, width : widthOfCard, height : heightOfCard)
cardView.backgroundColor = shop.data[index].color
}
}
и ShopClass
:
class ShopClass {
var data: [(id : Int, name : String, price : Double, color : UIColor)] = []
init(){
self.loadShopData()
}
func loadShopData(){
data.append((id : 1, name : "Pack 1", price : 4.99, color : UIColor(rgb: 0xE08B37)))
data.append((id : 2, name : "Pack 2", price : 9.99, color : UIColor(rgb: 0xAFAFAF)))
data.append((id : 3, name : "Pack 3", price : 15.99, color : UIColor(rgb: 0xF3CD00)))
data.append((id : 4, name : "Pack 4", price : 19.99, color : UIColor(rgb: 0xF20000)))
data.append((id : 5, name : "Pack 5", price : 24.99, color : UIColor(rgb: 0x000000)))
}
}
let shop = ShopClass()
рендер:
Последний цвет коричневый, но обычно черный.
Где я ошибся?