主要源 3.2.4

主要源 3.2.4

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最后发布2017年3月
SwiftSwift 版本3.0
SPM支持 SPM

Sam Williams 维护。



主要源 3.2.4

  • 编者:
  • Sam Williams

PrimarySource

概述

这是一个用于设置 UITableViews 和 UICollectionViews 数据源的 Swift 库。

用法

设置数据源

创建一个 DataSource 对象,并将其设置为您的 UITableView/UICollectionView 委托和数据源。一个 DataSource 包含分区,每个分区包含项。项负责配置单元格,这些单元格可能已被复用。

您可能需要一个方法来在新获取数据时重建数据源

func loadData()
    self.dataSource = DataSource()
    self.tableView.delegate = self.dataSource
    self.tableView.dataSource = self.dataSource

    self.dataSource <<< Section(title: "Form") { section in
        section <<< CollectionItem<TextFieldCell> { cell in
            cell.title = "Name"
            cell.onChange = { [unowned cell] in
                print("New value: \(cell.value)")
            }
        }
    }

    self.tableView.reloadData()
}

单元格注册

单元格可以通过几种方式创建

  • 通过类(纯粹从代码实例化)
section <<< CollectionItem<TextFieldCell>()
  • 通过Storyboard标识符(使用Storyboard中的动态原型)
section <<< CollectionItem<TextFieldCell>(storyboardIdentifier: "NormalTextCell")
  • 从NIB文件
section <<< CollectionItem<TextFieldCell>(nibName: "NormalTextCellNib")

单元格类型

包括了许多单元格类型。

显示单元格

  • UITableViewCell
  • SubtitleCell
  • ActivityIndicatorCell

交互式单元格

  • ButtonCell

字段单元格(每个都包含一个字段和一个强类型的 value

  • TextFieldCell (UITextField, String)
  • SwitchCell (UISwitch, Boolean)
  • DateFieldCell (UIDatePicker, NSDate)
  • PushSelectCell (选项类型集合,可在推送的视图中选择)
  • EmailAddressCell (String)
  • PhoneNumberCell (String)
  • IntegerCell (Int)
  • PasswordCell (String)
  • StepperCell (UIStepper, Int)
  • SliderCell (UISlider, Float)

单元格动作处理程序

可以为删除、触摸和重排序单元格等动作添加处理程序。添加处理程序将在 UI 中启用相应的动作:滑动删除将变为可能,单元格在触摸时会高亮,编辑模式下会出现重排序的辅助视图。

可以为单元格级别的动作添加处理程序

  • onDelete
  • onTap
section <<< TableViewItem<TextFieldCell> { cell in
    cell.title = "Name"
    // ...
}.onTap { _ in
    print("tapped") 
}.onDelete { _ in 
    print("deleted") 
}

以及分区级别的动作

  • onReorder
dataSource <<< Section { section in
    // ...
}.onReorder { sourceIndexPath, destinationIndexPath in 
    // ...
}

待处理

  • 支持UICollectionView