Created
September 24, 2018 20:43
-
-
Save trupin/2bb0eb472bc8b7f36d9ea4f592d35f23 to your computer and use it in GitHub Desktop.
This file contains 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
@objc final class ReviewTableViewCellViewModel: NSObject { | |
let content: String | |
let author: String | |
@objc(initWithReview:) init(_ review: Review) { | |
content = review.properties.content | |
author = review.properties.author | |
} | |
} | |
@objc final class ReviewTableViewCell: UITableViewCell { | |
private lazy var contentLabel: UILabel = { | |
let label = UILabel() | |
label.numberOfLines = 0 | |
label.textColor = .black | |
label.backgroundColor = .white | |
return label | |
}() | |
private lazy var authorLabel: UILabel = { | |
let label = UILabel() | |
label.numberOfLines = 1 | |
label.textColor = .black | |
label.backgroundColor = .white | |
label.font = UIFont.systemFont(ofSize: 17, weight: .bold) | |
return label | |
}() | |
override init(style: UITableViewCellStyle, reuseIdentifier: String?) { | |
super.init(style: style, reuseIdentifier: reuseIdentifier) | |
backgroundColor = .clear | |
contentView.addSubview(authorLabel) | |
authorLabel.translatesAutoresizingMaskIntoConstraints = false | |
contentView.addSubview(contentLabel) | |
contentLabel.translatesAutoresizingMaskIntoConstraints = false | |
NSLayoutConstraint.activate([ | |
authorLabel.topAnchor.constraintEqualToSystemSpacingBelow(contentView.topAnchor, multiplier: 1), | |
authorLabel.leadingAnchor.constraintEqualToSystemSpacingAfter(contentView.leadingAnchor, multiplier: 1), | |
contentView.trailingAnchor.constraintEqualToSystemSpacingAfter(authorLabel.trailingAnchor, multiplier: 1), | |
contentLabel.topAnchor.constraintEqualToSystemSpacingBelow(authorLabel.bottomAnchor, multiplier: 1), | |
contentView.bottomAnchor.constraintEqualToSystemSpacingBelow(contentLabel.bottomAnchor, multiplier: 1), | |
contentLabel.leadingAnchor.constraintEqualToSystemSpacingAfter(contentView.leadingAnchor, multiplier: 1), | |
contentView.trailingAnchor.constraintEqualToSystemSpacingAfter(contentLabel.trailingAnchor, multiplier: 1) | |
]) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
@objc(bindWithViewModel:) func bind(_ viewModel: ReviewTableViewCellViewModel) { | |
contentLabel.text = viewModel.content | |
authorLabel.text = "By \(viewModel.author)" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment