SwiftyHelpers 1.0.0

SwiftyHelpers 1.0.0

测试已测试
语言语言 SwiftSwift
许可协议 MIT
发布最新发布2016年9月
SPM支持 SPM

Jonathan Pacheco 维护。



SwiftyHelpers

SwiftHelpers 专注于使使用 Swift 进行开发的繁琐和重复任务变得更简单

要求

  • iOS 8.0+
  • XCode 8+

安装指南

动态库或使用 Swift 创建的库需要 iOS 8.0+

手动安装

只需要在终端中使用以下命令克隆项目

$ git clone https://github.com/jpachecou/SwiftyHelpers.git

找到名为 Source 的文件夹,并将其拖拽到你的项目中

如何使用 SwiftyHelpers

首先:如果你在用 CocoaPods,你只需要导入 import SwiftyHelpers 模块。

运算符和 UI 函数

  • UITableView & UICollectionView

    -为了使这个实用程序工作,必须在与子类具有相同名称的 .xib 文件中有一个名为的细胞。在我们给出的情况下的 tableView

    class FooTableViewCell: UITableViewCell {}
    class FooCollectionViewCell: UICollectionViewCell {}
    class FooTableHeaderView: UITableViewHeaderFooterView {}
    class FooCollectionHeaderView: UICollectionReusableView {}
    /*
        +-- FooTableViewCell.swift
        +-- FooTableViewCell.xib
        +-- FooCollectionViewCell.swift
        +-- FooCollectionViewCell.xib
        +-- FooTableHeaderView.swift
        +-- FooTableHeaderView.xib
        +-- FooCollectionHeaderView.swift
        +-- FooCollectionHeaderView.xib
    */

    一旦类和 .xib 准备好了,为了在 UITableViewUICollectionView 中注册细胞,只需在 tableView/collectionView 和细胞之间调用 <= 操作符。

    // Register cell in a table view
    self.tableView <= FooTableViewCell.self
    // Register header or footer in a table view
    self.tableView <= FooTableHeaderView.self
    
    // Register cell in collection view
    self.collectionView <= FooCollectionViewCell.self
    // Register header or footer in collection view
    collectionView <= (HeaderCollectionReusableView.self, UICollectionElementKindSectionHeader)
    collectionView <= (FooterCollectionReusableView.self, UICollectionElementKindSectionFooter)

    为了获得已注册的细胞,只需声明其类的一个实例。

    • 获取 tableView 中的细胞,例子

      func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
          let cell: FooTableViewCell = tableView.cellForClass()
          // configure cell
          return cell
      }
    • 获取 tableView 的头或尾,例子

      func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
          let cell: FooTableHeaderView = tableView.headerFooterForClass()
          // configure cell
          return cell
      }
    • 获取 collectionView 中的细胞,例子

      func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
          let cell: FooCollectionViewCell = collectionView.cellForClass(indexPath)
          // Configure cell
          return cell
      }
    • 获取 collection view 的头或尾,例子

      func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
          if kind == UICollectionElementKindSectionHeader {
              let header: HeaderCollectionReusableView = collectionView.supplementaryViewForClass(indexPath, kind: kind)
              // or
              let header: HeaderCollectionReusableView = headerForClass(indexPath)
              // Configure hedaer
              return header
          }
          let footer: FooterCollectionReusableView = footerForClass(indexPath)
          // Configure footer
          return footer
       }

  • UIView

    • 在加载 nib 文件之前,子类 UIView.xib 应具有相等的名称才能实例化它。

      class FooView: UIView {} 
      /*
          +-- FooView.swift
          +-- FooView.xib
      */
      
      guard let fooView: FooView? = loadCustomView() else { return } 
    • 简单地使用 removeAllSubViewsUIView 中递归地删除子视图。

    • 公共方法 getSubviewIntoView 递归使用,用于在 UIView 子视图内部获取一个特定子视图,具有一个具体的类。

      let fooSubview: FooView = getSubviewIntoView(self.view)

      这行将定位所有 UIView 子视图数组中的第一个 FooView 子视图

    许可协议

    SwiftyHelpers 在 MIT 许可下发行。有关详细信息,请参阅 LICENSE