Swift--面向协议开发

242 阅读1分钟

自定义协议

//
//  MGProtocol+extesnsion.swift
//  MGSDK
//
//  Created by Nicky Y M LIU_SP on 2021/1/11.
//  Copyright © 2021 Nicky Y M LIU_SP. All rights reserved.
//

import UIKit

public protocol MGReusableViewAble: class {}
extension MGReusableViewAble where Self: UIView {
    public static var reuseIdentifier: String {
        return String("\(self)")
    }
}

public protocol MGNibLoadAbleViewAble: class { }
extension MGNibLoadAbleViewAble where Self: UIView {
    public static func loadViewWithNib() -> Self {
        return Bundle.main.loadNibNamed("\(self)", owner: nil, options: nil)?.last! as! Self
    }
    
    public static var nibName: String {
        return String("\(self)")
    }
}


// MARK: - extension
extension UITableView {
    // Register
    public func registerCellFromNib<T: UITableViewCell>(_: T.Type) where T: MGReusableViewAble, T: MGNibLoadAbleViewAble {
        let Nib = UINib(nibName: T.nibName, bundle: nil)
        register(Nib, forCellReuseIdentifier: T.reuseIdentifier)
    }
    
    public func registerCell<T: UITableViewCell>(_: T.Type) where T: MGReusableViewAble {
        register(T.self, forCellReuseIdentifier: T.reuseIdentifier)
    }
    
    public func registerHeaderFooterFromNib<T: UITableViewHeaderFooterView>(_: T.Type) where T: MGReusableViewAble, T: MGNibLoadAbleViewAble {

        let Nib = UINib(nibName: T.nibName, bundle: nil)
        register(Nib, forHeaderFooterViewReuseIdentifier: T.reuseIdentifier)
    }
    
    public func registerHeaderFooter<T: UITableViewHeaderFooterView>(_: T.Type) where T: MGReusableViewAble {
        register(T.self, forHeaderFooterViewReuseIdentifier: T.reuseIdentifier)
    }
    
    public func dequeueReusableCell<T: UITableViewCell>(forIndexPath indexPath: IndexPath) -> T where T: MGReusableViewAble {
        guard let cell = dequeueReusableCell(withIdentifier: T.reuseIdentifier, for: indexPath) as? T else {
          fatalError("Could not dequeue cell with identifier: \(T.reuseIdentifier)")
      }
      return cell
    }
}

extension UICollectionView {
    // Register
    public func registerCellFromNib<T: UICollectionViewCell>(_: T.Type) where T: MGReusableViewAble, T: MGNibLoadAbleViewAble {
        let Nib = UINib(nibName: T.nibName, bundle: nil)
        register(Nib, forCellWithReuseIdentifier: T.reuseIdentifier)
    }
    
    public func registerCell<T: UICollectionViewCell>(_: T.Type) where T: MGReusableViewAble {
        register(T.self, forCellWithReuseIdentifier: T.reuseIdentifier)
    }
    
    
    // elementKindSectionHeader  elementKindSectionFooter
    public func registerHeaderFooter<T: UICollectionReusableView>(_: T.Type, elementKind: String) where T: MGReusableViewAble, T: MGNibLoadAbleViewAble {
        let Nib = UINib(nibName: T.nibName, bundle: nil)
        register(Nib, forSupplementaryViewOfKind: elementKind, withReuseIdentifier:  T.reuseIdentifier)
    }
    
    public func registerHeaderFooter<T: UICollectionReusableView>(_: T.Type, elementKind: String) where T: MGReusableViewAble {
        register(T.self, forSupplementaryViewOfKind: elementKind, withReuseIdentifier:  T.reuseIdentifier)
    }
    
    // dequeueReusable
    public func dequeueReusableCell<T: UICollectionViewCell>(forIndexPath indexPath: IndexPath) -> T where T: MGReusableViewAble {
        guard let cell = dequeueReusableCell(withReuseIdentifier: T.reuseIdentifier, for: indexPath) as? T else {
          fatalError("Could not dequeue cell with identifier: \(T.reuseIdentifier)")
      }
      return cell
    }
}


使用

import UIKit
import MGSDK

class MGTableViewCell: UITableViewCell, MGReusableViewAble  {

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}

import UIKit
import MGSDK

class MGTabViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white

        self.view.addSubview(self.tableView)
        tableView.leftAnchor.constraint(equalTo: self.view.leftAnchor,constant: 0).isActive = true
        tableView.topAnchor.constraint(equalTo: self.view.topAnchor,constant: 0).isActive = true
        tableView.rightAnchor.constraint(equalTo: self.view.rightAnchor,constant: 0).isActive = true
        tableView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor,constant: 0).isActive = true
    }
    
    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: self.view.frame, style: UITableView.Style.plain)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.tableFooterView = UIView()
        tableView.registerCell(MGTableViewCell.self)
        return tableView
    }()
}

extension MGTabViewController :UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        10
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(forIndexPath: indexPath) as MGTableViewCell
        return cell
    }
}

我的慕课网链接关于此文章