aboutsummaryrefslogtreecommitdiff
path: root/foray/Scenes/ForayTableViewCell.swift
blob: 3849169cfa613673b799acce67a14405b81000c0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//
//  ForayNewTableViewCell.swift
//  foray
//
//  Created by Nicholas Tay on 18/3/2022.
//

import UIKit
import SnapKit

class ForayTableViewCell: UITableViewCell {
    
    let container: UIView = UIView()
    
    let nameLabel: UILabel = {
        let l = UILabel()
        l.font = UIFont.preferredFont(forTextStyle: .headline)
        l.numberOfLines = 3
        l.adjustsFontForContentSizeCategory = true
        return l
    }()
    
    let descLabel: UILabel = {
        let l = UILabel()
        l.font = UIFont.preferredFont(forTextStyle: .caption1)
        l.adjustsFontForContentSizeCategory = true
        return l
    }()
    
    let itemImageView: UIImageView = {
        let iv = UIImageView()
        iv.contentMode = .scaleAspectFit
        return iv
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        initialiseViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("unreachable")
    }
    
    private func initialiseViews() {
        contentView.addSubview(container)
        container.addSubview(nameLabel)
        container.addSubview(descLabel)
        container.addSubview(itemImageView)
        setupConstraints()
    }
    
    private func setupConstraints() {
        container.snp.makeConstraints { (make) in
            make.edges.equalTo(contentView.snp.margins)
        }
        
        let imageWidth = 64
        let imageHeight = 38
        nameLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.leading.equalToSuperview()
            make.trailing.equalTo(itemImageView.snp.trailing).inset(imageWidth)
        }
        descLabel.snp.makeConstraints { (make) in
            make.top.equalTo(nameLabel.snp.bottom)
            make.leading.equalToSuperview()
            make.bottom.equalToSuperview()
            make.trailing.equalTo(itemImageView.snp.trailing).inset(imageWidth)
        }
        itemImageView.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.trailing.equalToSuperview()
            make.width.equalTo(imageWidth)
            make.height.equalTo(imageHeight)
        }
    }
    
    public func setData(name: String, desc: String, img: UIImage) {
        nameLabel.text = name
        descLabel.text = desc
        itemImageView.image = img
    }
}