Skip to content

Instantly share code, notes, and snippets.

@ppamorim
Created March 23, 2016 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ppamorim/08b4cd9778b0c9e23b5e to your computer and use it in GitHub Desktop.
Save ppamorim/08b4cd9778b0c9e23b5e to your computer and use it in GitHub Desktop.
import PureLayout
class TableViewMultiView : UIView {
var didLoadView = false
let tableView : UITableView = {
let tableView = UITableView.newAutoLayoutView()
tableView.keyboardDismissMode = .OnDrag
tableView.separatorInset.right = tableView.separatorInset.left
tableView.separatorColor = UIColor(rgba: "#D8D8D8")
tableView.hidden = true
tableView.tableFooterView = UIView(frame: CGRectZero)
return tableView
}()
let refreshControl : UIRefreshControl = {
let refreshControl = UIRefreshControl()
refreshControl.tintColor = UIColor(rgba:"#00ADDF")
refreshControl.backgroundColor = UIColor(rgba: "#EDEDED")
refreshControl.attributedTitle = NSAttributedString(string: "pull_to_refresh".localized,
attributes: [NSForegroundColorAttributeName: UIColor(rgba: "#00ADDF")])
return refreshControl
}()
let warningView : UILabel = {
let warningView = UILabel.newAutoLayoutView()
warningView.lineBreakMode = .ByWordWrapping
warningView.numberOfLines = 0
warningView.textAlignment = .Center
warningView.font = warningView.font.fontWithSize(14)
warningView.textColor = UIColor(rgba: "#3A3D3F")
warningView.backgroundColor = UIColor.whiteColor()
warningView.opaque = true
warningView.hidden = true
return warningView
}()
let actionButton : UIButton = {
let actionButton = UIButton.newAutoLayoutView()
actionButton.backgroundColor = UIColor(rgba: "#5BC9F3")
actionButton.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
actionButton.titleLabel!.font = actionButton.titleLabel!.font.fontWithSize(14)
actionButton.contentEdgeInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
actionButton.layer.cornerRadius = 5
actionButton.hidden = true
return actionButton
}()
let loadingView : UIActivityIndicatorView = {
let loadingView = UIActivityIndicatorView.newAutoLayoutView()
loadingView.color = UIColor(rgba: "#00ADDF")
loadingView.startAnimating()
return loadingView
}()
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor.whiteColor()
self.addSubview(loadingView)
self.addSubview(actionButton)
self.addSubview(warningView)
self.addSubview(tableView)
self.tableView.addSubview(refreshControl)
self.setNeedsUpdateConstraints()
self.updateConstraintsIfNeeded()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func updateConstraintsIfNeeded() {
if !didLoadView {
tableView.autoPinEdgesToSuperviewEdges()
warningView.autoPinEdgeToSuperviewEdge(.Left)
warningView.autoPinEdgeToSuperviewEdge(.Right)
warningView.autoAlignAxis(.Horizontal, toSameAxisOfView: self)
actionButton.autoPinEdge(.Top, toEdge: .Bottom, ofView: warningView, withOffset: 16.0)
actionButton.autoAlignAxis(.Vertical, toSameAxisOfView: self)
loadingView.autoCenterInSuperview()
didLoadView = true
}
super.updateConstraintsIfNeeded()
}
func showLoading() {
loadingView.startAnimating()
loadingView.hidden = false
tableView.hidden = true
warningView.hidden = true
actionButton.hidden = true
}
func reloadData() {
if self.refreshControl.refreshing {
self.refreshControl.endRefreshing()
}
tableView.reloadData()
tableView.hidden = false
warningView.hidden = true
actionButton.hidden = true
loadingView.hidden = true
loadingView.stopAnimating()
}
func showWarning(warning: String) {
tableView.hidden = true
warningView.hidden = false
warningView.text = warning
actionButton.hidden = true
loadingView.hidden = true
loadingView.stopAnimating()
}
func showError(target: AnyObject?, text: String, actionButtonText: String, actionButtonMethod: Selector) {
tableView.hidden = true
warningView.hidden = false
warningView.text = text
actionButton.hidden = false
actionButton.setTitle(actionButtonText, forState: .Normal)
actionButton.addTarget(target, action: actionButtonMethod, forControlEvents: .TouchUpInside)
loadingView.hidden = true
loadingView.stopAnimating()
}
func setTarget(target: AnyObject?, action: Selector) {
self.refreshControl.addTarget(target, action: action, forControlEvents: UIControlEvents.ValueChanged)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment