Last active
December 30, 2016 08:15
-
-
Save plummer/6e52ae58b9c490b030ea30da6e5b86bd to your computer and use it in GitHub Desktop.
A basic Eureka row for displaying a centred image in a row
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// LogoRow.swift | |
// | |
// Created by Andrew Plummer on 30/12/2016. | |
// | |
import Foundation | |
public protocol LogoCellType { | |
var logo: UIImageView { get } | |
} | |
open class _LogoCell<T>: Cell<T>, LogoCellType where T: Equatable { | |
public var logo: UIImageView | |
required public init(style: UITableViewCellStyle, reuseIdentifier: String?) { | |
self.logo = UIImageView() | |
logo.contentMode = .center | |
super.init(style: style, reuseIdentifier: reuseIdentifier) | |
} | |
open var dynamicConstraints = [NSLayoutConstraint]() | |
required public init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
open override func setup() { | |
super.setup() | |
selectionStyle = .none | |
height = { 280.0 } | |
self.logo.translatesAutoresizingMaskIntoConstraints = false | |
contentView.addSubview(logo) | |
setNeedsUpdateConstraints() | |
} | |
open override func update() { | |
super.update() | |
textLabel?.text = nil | |
detailTextLabel?.text = nil | |
if let rowValue = row.value as? UIImage { | |
logo.image = rowValue | |
} | |
} | |
open override func updateConstraints() { | |
customConstraints() | |
super.updateConstraints() | |
} | |
open func customConstraints() { | |
contentView.removeConstraints(dynamicConstraints) | |
dynamicConstraints = [] | |
let views : [String: AnyObject] = ["logo": logo] | |
dynamicConstraints.append( | |
contentsOf: NSLayoutConstraint | |
.constraints( | |
withVisualFormat: "V:|-[logo]-|", | |
options: [], | |
metrics: nil, | |
views: views) | |
) | |
dynamicConstraints.append( | |
contentsOf: NSLayoutConstraint | |
.constraints( | |
withVisualFormat: "H:|-[logo]-|", | |
options: [], | |
metrics: nil, | |
views: views) | |
) | |
contentView.addConstraints(dynamicConstraints) | |
} | |
} | |
open class LogoCell : _LogoCell<UIImage>, CellType { | |
required public init(style: UITableViewCellStyle, reuseIdentifier: String?) { | |
super.init(style: style, reuseIdentifier: reuseIdentifier) | |
} | |
required public init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
} | |
open class _LogoRow: Row<LogoCell> { | |
required public init(tag: String?) { | |
super.init(tag: tag) | |
} | |
} | |
public final class LogoRow: _LogoRow, RowType { | |
public required init(tag: String?) { | |
super.init(tag: tag) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment