美文网首页
Swift5.x入门22 -- 开发过程中遇到的问题记录

Swift5.x入门22 -- 开发过程中遇到的问题记录

作者: YanZi_33 | 来源:发表于2022-01-20 11:57 被阅读0次
    判断实例对象的类型
    • 现有SFHomeViewController继承自SFBaseTableViewController
    • 测试代码如下:
    class AppDelegate: UIResponder, UIApplicationDelegate {
        
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
            let vc = SFBaseTableViewController()
            
            if type(of: vc) == SFBaseTableViewController.self {
                print("1111") //打印
            }
            
            if type(of: vc) == SFHomeViewController.self {
                print("1111") //不打印
            }
            
            if vc.isMember(of: SFBaseTableViewController.self) {
                print("1111") //打印
            }
            
            if vc.isMember(of: SFHomeViewController.self) {
                print("1111")  //不打印
            }
            
            if vc.isKind(of: SFBaseTableViewController.self) {
                print("1111") //打印
            }
           
            if vc.isKind(of: SFHomeViewController.self) {
                print("1111") //不打印
            }
            
            if vc is SFBaseTableViewController {
                print("1111") //打印
            }
            
            if vc is SFHomeViewController {
                print("1111") //不打印
            }
        
            return true
        }
    }
    
    • type(of: 实例对象)vc.isMember(of: 元类型值)功能等价,实例对象只在当前类进行比较;
    • 实例对象 is 类名vc.isKind(of: 元类型值)功能等价,实例对象在其继承链上进行比较;
    • 针对iOS中私有类,例如UITableViewCellContentView,可采用view.isKind(of: NSClassFromString("UITableViewCellContentView")!),不能使用元类型(会报错);
    委托代理协议的使用
    • 自定义view,代码如下:
    protocol SFCateRightViewDelegate : NSObjectProtocol {
        func headerRefresh() -> Void
        func footerRefresh() -> Void
        func didSelectedSmallCategory(smallCategory: CategorySmallModel) -> Void
    }
    
    class SFCateRightView: SFBaseView {
        //代理属性 使用weak指针 解决循环引用问题
        weak var deletegate: SFCateRightViewDelegate?
    
        //下拉刷新
        func loadRefreshData() -> Void {
            deletegate?.headerRefresh()
        }
        
        //上拉加载
        func loadMoreData() -> Void {
            deletegate?.footerRefresh()
        }
        
        private lazy var header: SFRefreshHeader = {
            let header = SFRefreshHeader(refreshingBlock: { [weak self] in
                self?.loadRefreshData()
            })
            return header
        }()
    
        private lazy var footer: SFRefreshFooter = {
            let footer = SFRefreshFooter(refreshingBlock: { [weak self] in
                self?.loadMoreData()
            })
            return footer
        }()
    }
    
    • 控制器代码如下:
    import UIKit
    
    class SFCategoryController: SFBaseViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            self.isNavBackItemHidden = true
            self.loadCategory()
        }
        
        override func createUI() {
            self.view.addSubview(self.rightView)
            self.rightView.deletegate = self
        }
        
        override func layoutSubControls() {
            self.rightView.snp.makeConstraints { (make) in
                make.top.equalTo(CGFloat(Navigation_Bottom_Y))
                make.right.equalTo(self.view)
                make.height.equalTo(kScreenHeight - kTabbarHeight - Navigation_Bottom_Y)
                make.width.equalTo(kScreenWidth/4*3)
            }
        }
        
        //MARK: -- lazy
        private lazy var rightView: SFCateRightView = {
            let rightView = SFCateRightView()
            rightView.backgroundColor = UIColor.white
            return rightView
        }()
    }
    
    extension SFCategoryController: SFCateRightViewDelegate {
        func didSelectedSmallCategory(smallCategory: CategorySmallModel) {
            self.page = 1
            self.isDownRefresh = true
            loadGoods(categoryId: self.selectedSmallCategoryModel!.mallCategoryId!, categorySubId: smallCategory.mallSubId!, page: self.page)
        }
        
        func headerRefresh() {
            self.page = 1
            self.isDownRefresh = true
            loadGoods(categoryId: self.selectedCategoryModel!.mallCategoryId!, categorySubId: self.selectedSmallCategoryModel!.mallSubId!, page: self.page)
        }
        
        func footerRefresh() {
            self.page += 1
            self.isDownRefresh = false
            loadGoods(categoryId: self.selectedCategoryModel!.mallCategoryId!, categorySubId: self.selectedSmallCategoryModel!.mallSubId!, page: self.page)
        }
    }
    
    函数闭包使用 等价于OC中的Block
    • 自定义view,代码如下:
    import UIKit
    
    class SFCateLeftView: SFBaseView {
        //定义函数闭包    
        var didSelectCategoryBlock: ((SFCategoryModel,IndexPath) -> Void)?
        
        override func createUI() {
            self.addSubview(self.tableView)
        }
    
        override func layoutSubControls() {
            self.tableView.snp.makeConstraints { (make) in
                make.top.left.bottom.equalTo(self)
                make.width.equalTo(kScreenWidth/4-1)
            }
        }
        
        //MARK: --lazy
        private lazy var tableView: UITableView = {
            let tableView = UITableView(frame: .zero, style: UITableView.Style.plain)
            tableView.delegate = self
            tableView.dataSource = self
            tableView.backgroundColor = UIColor.white
            tableView.separatorStyle = .singleLine
            tableView.separatorColor = SF_LineColor_one
            tableView.separatorInset = UIEdgeInsets(top: 0, left: 15, bottom: 0, right: 0)
            if #available(iOS 11.0, *) {
                tableView.estimatedRowHeight = 0
                tableView.estimatedSectionHeaderHeight = 0
                tableView.estimatedSectionFooterHeight = 0
                tableView.contentInsetAdjustmentBehavior = .never
            } else {
                
            }
            //可隐藏没有数据的分割线
            let view = UIView()
            view.backgroundColor = UIColor.red
            tableView.tableFooterView = view;
            tableView.register(SFCateLeftCell.self, forCellReuseIdentifier: SFCateLeftCell.identifier())
            return tableView
        }()
    }
    
    extension SFCateLeftView: UITableViewDataSource,UITableViewDelegate {
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return self.datas?.count ?? 0
        }
        
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: SFCateLeftCell.identifier(), for: indexPath) as? SFCateLeftCell
            let model = self.datas![indexPath.row]
            cell!.model = model
            return cell!
        }
        
        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            return 44.0
        }
        
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            print(indexPath.row)
            let model = self.datas![indexPath.row]
            model.isSelected = true
            let preModel: SFCategoryModel = self.datas![self.selectedIndex!]
            preModel.isSelected = false
            self.selectedIndex = indexPath.row
            tableView.reloadData()
            //调用函数闭包
            self.didSelectCategoryBlock?(model,indexPath)
        }
    }
    
    • 控制器代码:
    import UIKit
    
    class SFCategoryController: SFBaseViewController {
        override func viewDidLoad() {
            super.viewDidLoad()
            self.isNavBackItemHidden = true
            self.loadCategory()
        }
        
        override func createUI() {
            self.view.addSubview(self.leftView)
            //初始化函数闭包
            self.leftView.didSelectCategoryBlock = { [weak self] (model: SFCategoryModel,indexPath: IndexPath) in
                print(model.mallCategoryName!)
                guard let strongSelf = self else { return }
                
                let smallCategorys = model.bxMallSubDto
                //刷新右侧顶部数据
                strongSelf.rightView.smallCategorys = smallCategorys
                strongSelf.selectedCategoryModel = model
                strongSelf.selectedSmallCategoryModel = strongSelf.rightView.selectedSmallCategoryModel
                strongSelf.isDownRefresh = true
                strongSelf.page = 1
                //加载商品数据
                strongSelf.loadGoods(categoryId: model.mallCategoryId!, categorySubId: strongSelf.selectedSmallCategoryModel!.mallSubId!, page: strongSelf.page)
            }
        }
        
        override func layoutSubControls() {
            self.leftView.snp.makeConstraints { (make) in
                make.left.equalTo(self.view)
                make.top.equalTo(CGFloat(Navigation_Bottom_Y))
                make.height.equalTo(kScreenHeight - kTabbarHeight - Navigation_Bottom_Y)
                make.width.equalTo(kScreenWidth/4)
            }
        }
        
        //MARK: -- lazy
        private lazy var leftView: SFCateLeftView = {
            let leftView = SFCateLeftView()
            leftView.backgroundColor = UIColor.red
            return leftView
        }()
    }
    

    相关文章

      网友评论

          本文标题:Swift5.x入门22 -- 开发过程中遇到的问题记录

          本文链接:https://www.haomeiwen.com/subject/qgrjhrtx.html