美文网首页
Swift泛型高阶使用-自定义dataSource 几行代码完成

Swift泛型高阶使用-自定义dataSource 几行代码完成

作者: J扣歪 | 来源:发表于2020-11-26 11:47 被阅读0次

tableView代码展示

        tableView = UITableView()
        self.view = tableView
        
        dataSource = TableViewNormalDataSource.init(configureCell: { (table, model, indexPath) -> UITableViewCell in
            var cell = table.dequeueReusableCell(withIdentifier: "cell_id")
            if cell == nil {
               cell = UITableViewCell.init(style: .subtitle, reuseIdentifier: "cell_id")
            }
            cell?.textLabel?.text = (model?.title ?? "") + "\(indexPath.row)"
            cell?.detailTextLabel?.text = (model?.subTitle ?? "") + "\(indexPath.row)"
            return cell!
        })
        dataSource?.configureRowHeight({ (table, model, indexPath) -> CGFloat in
            100
        })
        tableView?.dataSource = dataSource
        tableView?.delegate = dataSource
        
        self.loadData()

collectionView代码展示

        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 1
        layout.minimumInteritemSpacing = 1
        collectionView = UICollectionView.init(frame: .zero,collectionViewLayout: layout)
        collectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell_id")
        collectionView?.backgroundColor = .white
        self.view = collectionView
        
        dataSource = CollectionViewNormalDataSource.init(configureItem: { (collection, model, indexPath) -> UICollectionViewCell in
            let cell = collection.dequeueReusableCell(withReuseIdentifier: "cell_id", for: indexPath)
            cell.backgroundColor = model
            return cell
        })
        dataSource?.configureItemSize({ (collection, collectionViewLayout, model, indexPath) -> CGSize in
            CGSize(width: (UIScreen.main.bounds.size.width/3) - 2, height: UIScreen.main.bounds.size.width/3)
        })
        
        dataSource?.didSelectItem({[weak self] (collection, model, indexPath) in
            let alert = UIAlertController.init(title: nil, message: "你点击了第\(indexPath.row+1)个", preferredStyle: .alert)
            let action = UIAlertAction.init(title: "确定", style: .cancel, handler: nil)
            alert.addAction(action)
            self?.present(alert, animated: true, completion: nil)
        })
        
        collectionView?.delegate = dataSource
        collectionView?.dataSource = dataSource
        dataSource?.addData(colors)

demo 地址

https://github.com/jqygithud/SwiftDataSource.git

相关文章

  • Swift泛型高阶使用-自定义dataSource 几行代码完成

    tableView代码展示 collectionView代码展示 demo 地址 https://github.c...

  • 使用Web浏览器编译Swift代码,及Swift中的泛型

    使用Web浏览器编译Swift代码,及Swift中的泛型 使用Web浏览器编译Swift代码,及Swift中的泛型

  • OneDayOneSwift[23] - Generics

    泛型是 Swift 的强大特性之一,许多 Swift 标准库是通过泛型代码构建的。事实上,泛型的使用贯穿了整本语言...

  • Swift学习:泛型

    本篇将详细总结介绍Swift泛型的用法;Swift泛型代码让你能够根据自定义的需求,编写出适用于任意类型、灵活可重...

  • 008-自定义泛型,Collections

    自定义泛型 泛型类 代码实现 测试 泛型接口 代码实现 泛型方法 代码演示 测试 泛型上下边界 Collectio...

  • 2021-12-01

    swift5基本语法-泛型函数和泛型类型 Swift中泛型可以将类型参数化,提高代码复用率,减少代码量。 一、泛型...

  • Kotlin泛型 (4)泛型接口

      自定义泛型接口和自定义泛型类声明方式完全一样。以下是对上一章节泛型类代码的修改,配合使用泛型接口实现队列功能。...

  • Swift-泛型笔记

    Swift 泛型 Swift 提供了泛型让你写出灵活且可重用的函数和类型。 Swift 标准库是通过泛型代码构建出...

  • swift泛型

    一、以泛型为参数的函数泛型是Swift语言强大的核心,泛型是对类型的抽象,使用泛型开发者可以更加灵活方便的表达代码...

  • 探秘 Java 中的泛型(Generic)

    本文包括:JDK5之前集合对象使用问题泛型的出现泛型应用泛型典型应用自定义泛型——泛型方法自定义泛型——泛型类泛型...

网友评论

      本文标题:Swift泛型高阶使用-自定义dataSource 几行代码完成

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