From a44529a1c62ba58ed09c029f2a0f9c9e36099f21 Mon Sep 17 00:00:00 2001 From: Nicholas Tay Date: Fri, 18 Mar 2022 14:48:57 +1100 Subject: Add back in description label and image to custom cell I don't think this 100% is the correct way with SnapKit. Need to seek advice and improve the constraints. However, an improvement over before is that the accessibility font sizes actually do work as intended now! Now to add back in the segues somehow... --- foray/ForayNewTableViewCell.swift | 55 ++++++++++++++++++++++++++++++++---- foray/ForayTableViewController.swift | 26 ++++++++--------- 2 files changed, 62 insertions(+), 19 deletions(-) (limited to 'foray') diff --git a/foray/ForayNewTableViewCell.swift b/foray/ForayNewTableViewCell.swift index b2b22af..e679a93 100644 --- a/foray/ForayNewTableViewCell.swift +++ b/foray/ForayNewTableViewCell.swift @@ -10,7 +10,27 @@ import SnapKit class ForayNewTableViewCell: UITableViewCell { - let nameLabel: UILabel = UILabel() + let container: UIView = UIView() + + let nameLabel: UILabel = { + let l = UILabel() + l.font = UIFont.preferredFont(forTextStyle: .headline) + l.adjustsFontForContentSizeCategory = true + return l + }() + + let descLabel: UILabel = { + let l = UILabel() + l.font = UIFont.preferredFont(forTextStyle: .body) + 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) @@ -22,18 +42,41 @@ class ForayNewTableViewCell: UITableViewCell { } private func initialiseViews() { - contentView.addSubview(nameLabel) + contentView.addSubview(container) + container.addSubview(nameLabel) + container.addSubview(descLabel) + container.addSubview(itemImageView) setupConstraints() } private func setupConstraints() { + container.snp.makeConstraints { (make) in + make.top.bottom.equalToSuperview() + make.trailing.leading.equalToSuperview().inset(12) // better way to do this? + } + nameLabel.snp.makeConstraints { (make) in - make.top.bottom.equalToSuperview().inset(8) - make.leading.trailing.equalToSuperview().inset(8) + make.top.equalToSuperview().inset(8) + make.leading.equalToSuperview().inset(8) + make.trailing.equalTo(itemImageView.snp.trailing).inset(75) // better way to do this? + } + descLabel.snp.makeConstraints { (make) in + make.top.equalTo(nameLabel.snp.bottom).offset(8) + make.leading.equalToSuperview().inset(8) + make.bottom.equalToSuperview().inset(8) + make.trailing.equalTo(itemImageView.snp.trailing).inset(75) // better way to do this? + } + itemImageView.snp.makeConstraints { (make) in + make.top.equalToSuperview().inset(8) + make.trailing.equalToSuperview().inset(8) + make.width.equalTo(68) + make.height.equalTo(44) } } - public func setData(str: String) { - nameLabel.text = str + public func setData(name: String, desc: String, img: UIImage) { + nameLabel.text = name + descLabel.text = desc + itemImageView.image = img } } diff --git a/foray/ForayTableViewController.swift b/foray/ForayTableViewController.swift index 8f11285..6a0baa1 100644 --- a/foray/ForayTableViewController.swift +++ b/foray/ForayTableViewController.swift @@ -55,6 +55,8 @@ class ForayTableViewController: UITableViewController { override func viewDidLoad() { super.viewDidLoad() + tableView.rowHeight = UITableView.automaticDimension + // Register our custom cell tableView.register(ForayNewTableViewCell.self, forCellReuseIdentifier: "ForayNewTableViewCell") @@ -153,21 +155,19 @@ class ForayTableViewController: UITableViewController { override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let item = self.sections[indexPath.section].items[indexPath.row] -// let cell: ForayTableViewCell -// switch item.type { -// case .item: -// cell = tableView.dequeueReusableCell(withIdentifier: "ForayCell", for: indexPath) as! ForayTableViewCell -// cell.cellItemImage?.image = UIImage(named: item.id) -// case .quest: -// cell = tableView.dequeueReusableCell(withIdentifier: "ForayQuestCell", for: indexPath) as! ForayTableViewCell -// } -// -// cell.cellItemName?.text = item.name -// cell.cellItemSubtitle?.text = "ID: " + item.id + let type: String + let icon: UIImage + switch item.type { + case .item: + type = "Item" + icon = UIImage(named: item.id)! + case .quest: + type = "Quest" + icon = UIImage(named: "spy")! + } let cell: ForayNewTableViewCell = tableView.dequeueReusableCell(withIdentifier: "ForayNewTableViewCell", for: indexPath) as! ForayNewTableViewCell - cell.setData(str: item.name) - + cell.setData(name: item.name, desc: type + "ID: " + item.id, img: icon) return cell } -- cgit