控制器:
class CHSViewController:UIViewController {
//MARK: LifeCycle
deinit {
}
//视图初始化
override func loadView() {
super.loadView()
}
// 当加载视图结束时调用该方法
override func viewDidLoad() {
super.viewDidLoad()
}
// 视图将要显示时调用该方法
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
// 当视图已经显示时调用该方法
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
// 当视图将要消失时调用该方法
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
}
// 当时图已经消失时调用该方法
override func viewDidDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
}
// 当接收到内存警告时会执行这个方法
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
//MARK: Public Method
//MARK: Private Method
func setUI() {
self.setMas();
}
func setMas() {
}
//MARK: lazy load
lazy var chsView: CHSView = {[weak self] in
let chsView = CHSView(frame:.zero)
chsView.delegate = self
chsView.handle = { [weak self] in
}
return chsView
}()
}
//MARK: IB-Action
extension CHSViewController {
}
//MARK: Notice
extension CHSViewController {
}
//MARK: Delegate
extension CHSViewController {
}
视图:
import UIKit
typealias CHSHandle = () -> Void
protocol CHSViewDelegate: AnyObject {
func toOperate(view:CHSView)
}
class CHSView: UIView {
var handle: CHSHandle?
weak var chsViewDelegate : CHSViewDelegate?
override init(frame: CGRect) {
super.init(frame: frame)
self.setUI()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//MARK: Public Method
//MARK: Private Method
//MARK: lazy load
}
//MARK: Public Method
extension CHSView {
func setUI() {
self.setMas()
}
func setMas() {
}
}
//MARK: IB-Action
extension CHSView {
}
//MARK: Notice
extension CHSView {
}
//MARK: Delegate
extension CHSView {
}
Block(闭包):
typealias Block = () -> Void
public typealias CHSSetImageHandle = (_ model:Bool,_ messageId:String)->Void
public typealias CHSDidSelectItemHandle = (Int) -> Void
var block:Block?
var handle:CHSSetImageHandle?
guard let block = block else { return }
block()
view.block = { [weak self] in
}
view.block = { [weak self] index in
}
view.handle = { (_ model:Bool,_ messageId:String) -> Void in
}
static func checkVersion(callback:@escaping (_ isBoolean:Bool)->());
CHSTool.checkVersion { (isBoolean) in
if (!isBoolean) {
}
}
Protocol(代理):
protocol CHSViewDelegate: class {
func toOperate(uiView:UIView)
}
weak var chsViewDelegate : CHSViewDelegate?
self.chsViewDelegate?.toOperate(uiView: self);
view.chsViewDelegate = self;
extension viewController:CHSViewDelegate {
}
NotificationCenter(通知):
NotificationCenter.default.addObserver(self, selector: #selector(excuteLogic(_:)), name: NSNotification.Name("ABC"), object: nil)
NotificationCenter.default.post(name: NSNotification.Name("ABC"), object: self, userInfo: nil)
@objc private func excuteLogic(_ notification: Notification) {
if notification.name == Notification.Name.MGTask.BecomeActive {
}
}
deinit {
NotificationCenter.default.removeObserver(self);
}
自定义视图(懒加载):
lazy var chsView: CHSView = {[weak self] in
let chsView = CHSView(frame:.zero)
chsView.delegate = self
chsView.freshBlock = { [weak self] in
}
chsView.loadBlock = { [weak self] in
}
return chsView
}()
}
UIImageView(懒加载):
lazy var exampleImageView:UIImageView = {
let exampleImageView = UIImageView()
exampleImageView.image = UIImage.init(named: "sth")
return exampleImageView
}()
UILabel(懒加载):
lazy var exampleLabel:UILabel = {
let exampleLabel = UILabel()
exampleLabel.font = UIFont.systemFont(ofSize: 16)
exampleLabel.textColor = .red
return exampleLabel
}()
UIView(懒加载):
lazy var exampleView:UIView = {
let exampleView = UIView()
return example
}()
UIButton(懒加载):
lazy var exampleButton:UIButton = {
let exampleButton = UIButton()
exampleButton.setTitle("即将上线", for: .normal);
exampleButton.setTitleColor(.red, for: .normal)
exampleButton.titleLabel?.font = UIFont.systemFont(ofSize: 16);
exampleButton.contentHorizontalAlignment = .left;
return exampleButton
}()
UITableView(懒加载):
private lazy var tableView:UITableView = {
let tableView = UITableView(frame: .zero, style: .grouped)
tableView.delegate = self
tableView.dataSource = self
tableView.backgroundColor = .clear;
tableView.tableHeaderView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: 0, height: CGFloat.leastNormalMagnitude));
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "uiTableViewCell")
return tableView
}()
UICollectionView(懒加载):
private lazy var collectionView: UICollectionView = {
let layout=UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
collectionView.backgroundColor =.clear;
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "uiCollectionReusableView")
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "uiCollectionViewCell")
collectionView.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "uiCollectionReusableView")
collectionView.showsVerticalScrollIndicator=false
collectionView.showsHorizontalScrollIndicator=false
collectionView.contentInset = UIEdgeInsetsMake(self.itemOtherHeight, 10, 10, 10);
collectionView.mj_header = self.refreshHeader;
collectionView.mj_header?.ignoredScrollViewContentInsetTop = self.itemOtherHeight;
collectionView.mj_footer = self.refreshFooter;
return collectionView
}()
MGRefreshHeader(懒加载):
fileprivate lazy var refreshHeader: MGRefreshHeader = {
let refreshHeader = MGRefreshHeader(refreshingTarget: self, refreshingAction: #selector(fireFresh))
return refreshHeader
}()
MGRefreshFooter(懒加载):
fileprivate lazy var refreshFooter: MGRefreshFooter = {
let refreshFooter = MGRefreshFooter(refreshingTarget: self, refreshingAction: #selector(fireMore))
refreshFooter.isHidden = true;
return refreshFooter
}()
UITableViewDelegate:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
return headerView
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerView = UIView()
return footerView
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 0.00001
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 10
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
UITableViewDataSource:
func numberOfSections(in tableView: UITableView) -> Int {
return 10;
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let viewCell = UITableViewCell(style:UITableViewCell.CellStyle.default, reuseIdentifier: "uITableViewCell")
viewCell.selectionStyle = .none;
return viewCell;
}
GCD(异步回到主线程):
DispatchQueue.main.async {
}
GCD(延时):
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.5) {
}
网友评论