Skip to content

Instantly share code, notes, and snippets.

@nzatsepilov
Created February 22, 2018 10:54
Show Gist options
  • Save nzatsepilov/745c8e19259983ad8b4b843a25f69abd to your computer and use it in GitHub Desktop.
Save nzatsepilov/745c8e19259983ad8b4b843a25f69abd to your computer and use it in GitHub Desktop.
Example cell
//
// OrderTableViewCell.swift
// foamline
//
// Created by Albert Musagitov on 18/11/2017.
// Copyright © 2017 BytePace. All rights reserved.
//
import UIKit
// MARK: - OrderTableViewCell
class OrderTableViewCell: UITableViewCell {
static let nibName = "OrderTableViewCell"
static let reuseId = "OrderTableViewCell"
@IBOutlet fileprivate weak var statusLabel: UILabel!
@IBOutlet fileprivate weak var nameLabel: UILabel!
@IBOutlet fileprivate weak var dateLabel: UILabel!
@IBOutlet fileprivate weak var contragentLabel: UILabel!
@IBOutlet fileprivate weak var orderPriceLabel: UILabel!
@IBOutlet weak var orderMenuButtonImageView: UIImageView!
weak var delegate: OrderTableViewCellDelegate?
var order: Order? {
didSet {
didSetOrder()
}
}
// MARK: - Lifecycle
override func awakeFromNib() {
super.awakeFromNib()
setup()
}
override func prepareForReuse() {
super.prepareForReuse()
clean()
}
// MARK: - Setters
fileprivate func didSetOrder() {
refresh()
}
// MARK: - IBActions
@IBAction fileprivate func orderMenuButtonSelected(_ sender: Any) {
delegate?.orderTableViewCellDidRequestToShowMenu(self)
}
@IBAction fileprivate func detailsButtonSelected(_ sender: Any) {
delegate?.orderTableViewCellDidRequestShowDetails(self)
}
// MARK: - Fileprivate
fileprivate func setup() {
clean()
}
fileprivate func clean() {
statusLabel.text = nil
nameLabel.text = nil
dateLabel.text = nil
contragentLabel.text = nil
orderPriceLabel.text = nil
}
fileprivate func refresh() {
guard let order = self.order else {
clean()
return
}
if let orderStatus = order.status {
statusLabel.text = orderStatus.name ?? "?"
statusLabel.textColor = orderStatus.nativeColor
} else {
statusLabel.text = "Новый"
statusLabel.textColor = UIColor.foamlineActiveGreen
}
if let orderNumber = order.number {
// Format: "0000042766(30Б-СП-/437)"
if let orderNumberBase = order.numberBase {
nameLabel.text = "\(orderNumber) (\(orderNumberBase))"
} else {
nameLabel.text = orderNumber
}
} else if let startDate = order.orderDate {
// Format: "Заказ от 01.01.2017"
nameLabel.text = "Заказ от \(startDate.toString(format: .custom("dd.MM.yyyy")))"
}
dateLabel.text = order.orderDate?.toString(format: .custom("dd.MM.yyyy")) ?? "?"
contragentLabel.text = order.contragent?.name ?? "?"
orderPriceLabel.text = order.summaryPrice > 0 ? String(format: "%.2f", order.summaryPrice) : "0"
}
}
// MARK: - OrderTableViewCellDelegate
protocol OrderTableViewCellDelegate: NSObjectProtocol {
func orderTableViewCellDidRequestShowDetails(_ cell: OrderTableViewCell)
func orderTableViewCellDidRequestToShowMenu(_ cell: OrderTableViewCell)
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
setup()
...
}
fileprivate func setup() {
tableView.register(UINib(nibName: OrderTableViewCell.nibName, bundle: nil), forCellReuseIdentifier: OrderTableViewCell.reuseId)
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment