|
1 | 1 | // |
2 | 2 | // StationTableViewCell.swift |
3 | | -// Swift Radio |
| 3 | +// SwiftRadio |
4 | 4 | // |
5 | | -// Created by Matthew Fecher on 4/4/15. |
6 | | -// Copyright (c) 2015 MatthewFecher.com. All rights reserved. |
| 5 | +// Created by Fethi El Hassasna on 2023-06-24. |
| 6 | +// Copyright © 2023 matthewfecher.com. All rights reserved. |
7 | 7 | // |
8 | 8 |
|
9 | 9 | import UIKit |
| 10 | +import NVActivityIndicatorView |
10 | 11 |
|
11 | 12 | class StationTableViewCell: UITableViewCell { |
12 | | - |
13 | | - @IBOutlet weak var stationNameLabel: UILabel! |
14 | | - @IBOutlet weak var stationDescLabel: UILabel! |
15 | | - @IBOutlet weak var stationImageView: UIImageView! |
| 13 | + |
| 14 | + let stationImageView: UIImageView = { |
| 15 | + let imageView = UIImageView() |
| 16 | + imageView.contentMode = .scaleAspectFill |
| 17 | + imageView.clipsToBounds = true |
| 18 | + NSLayoutConstraint.activate([ |
| 19 | + imageView.heightAnchor.constraint(equalToConstant: 75), |
| 20 | + imageView.widthAnchor.constraint(equalToConstant: 110) |
| 21 | + ]) |
| 22 | + return imageView |
| 23 | + }() |
| 24 | + |
| 25 | + let titleLabel: UILabel = { |
| 26 | + let label = UILabel() |
| 27 | + label.font = .preferredFont(forTextStyle: .title3) |
| 28 | + label.numberOfLines = 2 |
| 29 | + label.translatesAutoresizingMaskIntoConstraints = false |
| 30 | + return label |
| 31 | + }() |
| 32 | + |
| 33 | + let subtitleLabel: UILabel = { |
| 34 | + let label = UILabel() |
| 35 | + label.font = .preferredFont(forTextStyle: .footnote) |
| 36 | + label.translatesAutoresizingMaskIntoConstraints = false |
| 37 | + return label |
| 38 | + }() |
| 39 | + |
| 40 | + override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { |
| 41 | + super.init(style: style, reuseIdentifier: reuseIdentifier) |
| 42 | + setupViews() |
| 43 | + } |
| 44 | + |
| 45 | + override func prepareForReuse() { |
| 46 | + super.prepareForReuse() |
| 47 | + titleLabel.text = nil |
| 48 | + subtitleLabel.text = nil |
| 49 | + stationImageView.image = nil |
| 50 | + } |
| 51 | + |
| 52 | + required init?(coder: NSCoder) { |
| 53 | + fatalError("init(coder:) has not been implemented") |
| 54 | + } |
| 55 | + |
| 56 | + private func setupViews() { |
16 | 57 |
|
17 | | - override func awakeFromNib() { |
18 | | - super.awakeFromNib() |
| 58 | + selectionStyle = .default |
19 | 59 |
|
20 | | - // set ImageView shadow |
21 | | - stationImageView.applyShadow() |
| 60 | + let vStackView = UIStackView(arrangedSubviews: [titleLabel, subtitleLabel]) |
| 61 | + vStackView.spacing = 8 |
| 62 | + vStackView.axis = .vertical |
| 63 | + vStackView.translatesAutoresizingMaskIntoConstraints = false |
22 | 64 |
|
23 | | - // set table selection color |
24 | | - let selectedView = UIView(frame: CGRect.zero) |
25 | | - selectedView.backgroundColor = UIColor(red: 78/255, green: 82/255, blue: 93/255, alpha: 0.6) |
26 | | - selectedBackgroundView = selectedView |
| 65 | + let hStackView = UIStackView(arrangedSubviews: [stationImageView, vStackView]) |
| 66 | + hStackView.spacing = 8 |
| 67 | + hStackView.axis = .horizontal |
| 68 | + hStackView.alignment = .center |
| 69 | + hStackView.translatesAutoresizingMaskIntoConstraints = false |
| 70 | + |
| 71 | + contentView.addSubview(hStackView) |
| 72 | + |
| 73 | + NSLayoutConstraint.activate([ |
| 74 | + hStackView.topAnchor.constraint(equalTo: contentView.layoutMarginsGuide.topAnchor), |
| 75 | + hStackView.trailingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.trailingAnchor), |
| 76 | + hStackView.bottomAnchor.constraint(equalTo: contentView.layoutMarginsGuide.bottomAnchor), |
| 77 | + hStackView.leadingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leadingAnchor) |
| 78 | + ]) |
27 | 79 | } |
| 80 | +} |
28 | 81 |
|
| 82 | +extension StationTableViewCell { |
29 | 83 | func configureStationCell(station: RadioStation) { |
30 | 84 |
|
31 | 85 | // Configure the cell... |
32 | | - stationNameLabel.text = station.name |
33 | | - stationDescLabel.text = station.desc |
| 86 | + titleLabel.text = station.name |
| 87 | + subtitleLabel.text = station.desc |
34 | 88 |
|
35 | 89 | station.getImage { [weak self] image in |
36 | 90 | self?.stationImageView.image = image |
37 | 91 | } |
38 | 92 | } |
39 | | - |
40 | | - override func prepareForReuse() { |
41 | | - super.prepareForReuse() |
42 | | - stationNameLabel.text = nil |
43 | | - stationDescLabel.text = nil |
44 | | - stationImageView.image = nil |
45 | | - } |
46 | 93 | } |
0 commit comments