import UIKit
class MyTableViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource
{
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var collectionViewHeight: NSLayoutConstraint!
var images:[String] = []
override func awakeFromNib() {
super.awakeFromNib()
self.collectionView.delegate = self
self.collectionView.dataSource = self
self.collectionView!.register(UINib(nibName:"MyCollectionViewCell", bundle:nil),
forCellWithReuseIdentifier: "myCell")
}
func reloadData(title:String, images:[String]) {
self.titleLabel.text = title
self.images = images
self.collectionView.reloadData()
let contentSize = self.collectionView.collectionViewLayout.collectionViewContentSize
collectionViewHeight.constant = contentSize.height
self.collectionView.collectionViewLayout.invalidateLayout()
}
func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
return images.count
}
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCell",
for: indexPath) as! MyCollectionViewCell
cell.imageView.image = UIImage(named: images[indexPath.item])
return cell
}
override func draw(_ rect: CGRect) {
let lineWidth = 1 / UIScreen.main.scale
let lineAdjustOffset = 1 / UIScreen.main.scale / 2
let lineColor = UIColor(red: 0xe0/255, green: 0xe0/255, blue: 0xe0/255, alpha: 1)
guard let context = UIGraphicsGetCurrentContext() else {
return
}
let drawingRect = self.bounds.insetBy(dx: lineAdjustOffset, dy: lineAdjustOffset)
let path = CGMutablePath()
path.move(to: CGPoint(x: drawingRect.minX, y: drawingRect.maxY))
path.addLine(to: CGPoint(x: drawingRect.maxX, y: drawingRect.maxY))
context.addPath(path)
context.setStrokeColor(lineColor.cgColor)
context.setLineWidth(lineWidth)
context.strokePath()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}