I need to draw 5 different style of cell in a UITableView and i need a small help of you! I show you what i write in my ViewController:
var selfHeightCell : CGFloat? // for custom rowHeight
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.UITableView.estimatedRowHeight = 200.00;
self.selfHeightCell = UITableViewAutomaticDimension;
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cardCell", for: indexPath) as! CardCell
switch indexPath.row { // this is for test my code
case 0:
selfHeightCell = cell.workTest(drawCard: .verySmallCard)
case 1:
selfHeightCell = cell.workTest(drawCard: .smallCard)
case 2:
selfHeightCell = cell.workTest(drawCard: .mediumCard)
case 3:
selfHeightCell = cell.workTest(drawCard: .basiqCard)
case 4:
selfHeightCell = cell.workTest(drawCard: .bigCard)
case 5:
selfHeightCell = cell.workTest(drawCard: .mediumCard)
case 6:
selfHeightCell = cell.workTest(drawCard: .mediumCard)
case 7:
selfHeightCell = cell.workTest(drawCard: .basiqCard)
case 8:
selfHeightCell = cell.workTest(drawCard: .basiqCard)
case 9:
selfHeightCell = cell.workTest(drawCard: .basiqCard)
default:
selfHeightCell = cell.workTest(drawCard: .basiqCard)
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return selfHeightCell!
}
and now the code of my CardCell:
func workTest(drawCard: state) -> CGFloat{
var heightOfCardRow : CGFloat = 0
switch drawCard {
case .verySmallCard:
heightOfCardRow = self.drawCard(height : 60)
case .smallCard:
heightOfCardRow = self.drawCard(height : 100)
case .mediumCard:
heightOfCardRow = self.drawCard(height : 140)
case .basiqCard:
heightOfCardRow = self.drawCard(height : 200)
case .bigCard:
heightOfCardRow = self.drawCard(height : 280)
}
return heightOfCardRow
}
func drawCard(height : CGFloat) -> CGFloat{
if(cardView.subviews.count == 0){
self.addSubview(cardView)
cardView.frame = CGRect(marginCardWidth,
marginCardHeight,
self.bounds.size.width - (marginCardWidth*2),
height - (marginCardHeight*2))
cardView.layer.cornerRadius = 10
}
if(self.ShadowLayerCard == nil){
let shadowLayer = CAShapeLayer()
self.ShadowLayerCard = shadowLayer
shadowLayer.path = UIBezierPath(roundedRect: cardView.bounds, cornerRadius: 10).cgPath
shadowLayer.fillColor = UIColor(rgb: 0xffcc00).cgColor
shadowLayer.shadowPath = shadowLayer.path
shadowLayer.shadowColor = UIColor.black.cgColor
shadowLayer.shadowRadius = 5
shadowLayer.shadowOpacity = 0.2
shadowLayer.shadowOffset = CGSize(width: 0, height: 0)
cardView.layer.insertSublayer(shadowLayer, at: 0)
}
return cardView.bounds.size.height + (marginCardHeight*2)
}
I show you a picture of the simulator:
Sorry for all the code but it necessary I think to solve my problem. I think the error is because i draw programmatically or I custom the height of the cell in a wrong way!
My question is: What are the bad ways that give these problems?