1 Star 0 Fork 0

edwardyyk / YKSwiftSectionViewModel

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

YKSwiftSectionViewModel

Platform LicenseLicense

Requirements

  • iOS 9.0+
  • Xcode 9.3+
  • Swift 4.0+

插件示意图

Installation

YKSwiftSectionViewModel is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'YKSwiftSectionViewModel'

Then, run the following command:

$ pod install

Example

1、init


    public lazy var collectionView:YKSectionCollectionView = {
        let viewModel1 = ScTestViewModel.init()
        其中 frame  布局 datas为内部包含多少个sectionviewmodel
        let view = YKSectionCollectionView.init(frame: CGRect.init(x: 0, y: KSTATUSBAR_HEIGHT + 44, width: YKScreenW, height: YKScreenH - (KSTATUSBAR_HEIGHT + 44)),datas: [viewModel1])
        let headerView = UIView.init(frame: CGRect(x: 0, y: 0, width: view.bounds.size.width, height: 50)).yk_backgroundColor(.red)
        
        每个sectionviewmodel可设置的延迟时间
        view.outTime = 15
        
        //此方法为笔者自定义的头部刷新,用户可根据自己情况设置刷星
        view.addRefreshHeader { [weak self] in
            YKSwiftAlertCenter.showLoading(message: "正在加载")
            //主要方法为此refreshData告诉section刷新的模式(其中头部刷新和底部刷新)
            self!.collectionView.refreshData(mode: .Header)
        }
        
        //同上
        view.addRefreshFooter { [weak self] in
            self!.collectionView.refreshData(mode: .Footer)
        }
        
        //方法为结束刷新时候的动作 用户可根据自己的情况选择结束顶部和底部的刷新动画(笔者内容已经动态调用了reloadData,用户无需自行调用)
        view.toSetEndRefresh { [weak self] isNoMoreData in
            YKSwiftAlertCenter.dissLoading()
            if let strongself = self {
                strongself.collectionView.headerEndRefresh()
                strongself.collectionView.footerEndRefresh(noMorData: false)
            }
        }
        
        //方法为结束刷新时候进行了错误的报告,内部为笔者自定义的弹窗,用户可根据自己项目进行错误处理
        view.toSetErrorCallBack { error in
            YKSwiftAlertCenter.showMessage(message: error.localizedDescription)
        }
        
        //(重点)此方法为内部响应的操作,传递出来控制器进行分别的push或present出来
        view.toSetHandleViewController { [weak self] controller, type, animated in
            guard let weakself = self else { return }
            if type == .Push {
                weakself.navigationController?.pushViewController(controller, animated: animated)
            }else if type == .Present {
                weakself.present(controller, animated: animated, completion: nil)
            }
        }
        return view
    }()

2、initsubviewmodel

import YKSwiftSectionViewModel

class ScTestViewModel: YKSectionViewModelMainProtocol {
    
    private var count:Int = 0
    
    //  (必须实现) 当前section拥有多少个item
    func yksc_numberOfItem() -> Int {
        return self.count
    }
    
    //  (必须实现) 当前section获取数据源(网络请求等延时操作)
    func yksc_beginToReloadData(mode: YKSectionViewModelRefreshMode.RawValue, reloadCallBack: @escaping ((Bool) -> Void), errorCallBack: @escaping ((Error) -> Void)) {
       if mode == YKSectionViewModelRefreshMode.Header.rawValue {
           self.count += 5;
       }else if mode == YKSectionViewModelRefreshMode.Footer.rawValue {
           let error = NSError.init(domain: "YKSwiftSectionViewModel", code: -1, userInfo: [
               NSLocalizedDescriptionKey:"错误",
               NSLocalizedFailureReasonErrorKey:"错误",
               NSLocalizedRecoverySuggestionErrorKey:"请检查内容",
           ])
           errorCallBack(error)
       }
       reloadCallBack(true)
    }
    
    //  (必须实现) 当前section的indexPath用到的itemId
    func yksc_idForItem(at indexPath: IndexPath) -> String {
        if ((indexPath.row % 2) == 1) {
            return "ScTest2Cell"
        }
        return "ScTestCell"
    }
    
    //  (必须实现) 注册当前section所用到的所有item
    func yksc_registItems() -> [YKSectionResuseModel] {
        return [YKSectionResuseModel.init(className: ScTestCell.classForCoder(), classId: "ScTestCell"),YKSectionResuseModel.init(className: ScTest2Cell.classForCoder(), classId: "ScTest2Cell")]
    }
            
    //  (必须实现) 当前section的item的大小
    func yksc_sizeOfItem(with width: CGFloat, atIndexPath: IndexPath) -> CGSize {
        if ((atIndexPath.row % 2) == 1) {
            return CGSize(width: width, height: 50)
        }
        return CGSize(width: width, height: 70)
    }
        
    // 注册当前section所需要用到的header (无设置可忽略)
    func yksc_registHeader() -> [YKSectionResuseModel] {
        return [YKSectionResuseModel.init(className: SCTestHeaderView.classForCoder(), classId: "SCTestHeaderView")]
    }
        
    // 当前section所需要设置的header (无设置可忽略)
    func yksc_idForHeader() -> String {
        return "SCTestHeaderView"
    }
        
    // 当前section的header的大小 (无设置可忽略)
    func yksc_sizeOfHeader(width: CGFloat) -> CGSize {
        return CGSize(width: width, height: 100)
    }
    
    // 注册当前section所需要用到的footer (无设置可忽略)
    func yksc_registFooter() -> [YKSectionResuseModel] {
        return [YKSectionResuseModel.init(className: SCTestFooterView.classForCoder(), classId: "SCTestFooterView")]
    }
    
    // 当前section所需要设置的footer (无设置可忽略)
    func yksc_idForFooter() -> String {
        return "SCTestFooterView"
    }
    
    // 当前section的footer的大小 (无设置可忽略)
    func yksc_sizeOfFooter(width: CGFloat) -> CGSize {
        return CGSize(width: width, height: 30)
    }
    
    // 当前section其中cell被点击的响应
    func yksc_didSelectItem(at indexPath: IndexPath, collectionView: YKSectionCollectionView, callBack: ((UIViewController, YKSectionViewModelPushType, Bool) -> Void)) {
        
    }
    
    // 响应链传递(如没有可忽略)
    func yksc_handleRouterEvent(eventName: String, userInfo: Dictionary<String, Any>, collectionView: YKSectionCollectionView, callBack: ((UIViewController, YKSectionViewModelPushType, Bool) -> Void)) -> Bool {
        
        return false
    }
    
    func yksc_noDataShowHeaderFooter() -> Bool {
        return false
    }

3、initcell or header

cell

import YKSwiftSectionViewModel

class ScTest2Cell: YKSectionCollectionViewCell,YKSectionViewModelResuseProtocol {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        let view = UIView.init(frame: CGRect(x: 10, y: 10, width: self.bounds.size.width - 20, height: self.bounds.size.height - 20))
        view.backgroundColor = .yellow
        self.addSubview(view)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func loadData(_ viewModel: YKSectionCollectionViewProtocol, _ atIndexPath: IndexPath) {
        
    }
}

header

import YKSwiftSectionViewModel

class SCTestHeaderView: YKSectionCollectionHeaderFooterView,YKSectionViewModelResuseProtocol {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        let view = UIView.init(frame: CGRect(x: 10, y: 10, width: self.bounds.size.width - 20, height: self.bounds.size.height - 20))
        view.backgroundColor = .lightGray
        self.addSubview(view)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func loadDataWithSection(_ viewModel: YKSectionMainProtocol, _ atSection: Int) {
        
    }
    
    func loadDataWithIndexPath(_ viewModel: YKSectionMainProtocol, _ atIndexPath: IndexPath) {
        
    }
        
}

Author

edward, 534272374@qq.com

License

YKSwiftSectionViewModel is available under the MIT license. See the LICENSE file for more info.

Copyright (c) 2021 edward <534272374@qq.com> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Swift环境下 根据 viewModel进行的 section配置 展开 收起
Swift 等 2 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/Edwrard/YKSwiftSectionViewModel.git
git@gitee.com:Edwrard/YKSwiftSectionViewModel.git
Edwrard
YKSwiftSectionViewModel
YKSwiftSectionViewModel
master

搜索帮助