Last active
August 9, 2018 08:10
-
-
Save candostdagdeviren/883131aefba9530664cc774933346324 to your computer and use it in GitHub Desktop.
Custom View Implementation for iOS
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
import UIKit | |
protocol ViewCustomized { | |
func createView<T>(_ viewClass: T.Type, parentView: UIView) -> T where T: UIView, T: ViewRenderable | |
} | |
protocol ViewRenderable { | |
func render() | |
} | |
extension ViewCustomized { | |
func createView<T>(_ viewClass: T.Type, parentView: UIView) | |
-> T where T: UIView, T: ViewRenderable { | |
let v = viewClass.init(frame: parentView.bounds) | |
v.autoresizingMask = [.flexibleWidth, .flexibleHeight] | |
parentView.addSubview(v) | |
v.render() | |
return v | |
} | |
} |
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
class WebsitesListView: UIView { | |
private enum ViewConstants { | |
static let cellIdentifier = "cellIdentifier" | |
} | |
var viewData: WebsitesListViewData? | |
} | |
extension WebsitesListView: ViewRenderable { | |
func render() { | |
self.backgroundColor = UIColor.clear | |
// Similar things like below. Create views and add them using the WebsiteListView with using auto-layout | |
// self.tableView = createTableView() | |
// self.refreshControl = createRefreshControl() | |
} | |
} |
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
class WebsitesListViewController: UIViewController, ViewCustomized, AlertDisplayable { | |
private let viewModel: WebsitesListViewModel | |
private var websitesListView: WebsitesListView! | |
init(viewModel: WebsitesListViewModel) { | |
self.viewModel = viewModel | |
super.init(nibName: nil, bundle: nil) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.websitesListView = createView(WebsitesListView.self, | |
parentView: view) | |
websitesListView.viewData = viewModel | |
} | |
} |
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
protocol WebsitesListViewData: WebsiteNameTableViewCellData { | |
func numberOfRows() -> Int | |
func website(at indexPath: IndexPath) -> Website | |
} | |
class WebsitesListViewModel { | |
var websites: [Website] | |
init(websites: [Website]) { | |
self.websites = websites | |
} | |
} | |
extension WebsitesListViewModel: WebsitesListViewData { | |
func numberOfRows() -> Int { | |
return websites.count | |
} | |
func website(at indexPath: IndexPath) -> Website { | |
return websites[indexPath.row] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment