这是一个用于设置 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>()
section <<< CollectionItem<TextFieldCell>(storyboardIdentifier: "NormalTextCell")
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
// ...
}