FormKit 是一个 iOS 中用于轻松构建基于 UITableView 表单的 API。您创建代表您的表单的模型,然后 FormKit 会处理剩下的工作。
import FormKit
class LoginViewController: FormViewController {
// MARK: - UIViewController
override func viewDidLoad() {
super.viewDidLoad()
let section = FormSection()
section.appendFormRow(self.email)
section.appendFormRow(self.password)
let form = Form()
form.appendFormSection(section)
self.form = form
}
// MARK: - Properties
lazy private var email: FormTextField = {
let email = FormTextField()
email.title = "Email"
email.configureCell = { [unowned self] (cell) in
cell.textField.keyboardType = .EmailAddress
cell.textField.autocorrectionType = .No
cell.textField.autocapitalizationType = .None
}
return email
}()
lazy private var password: FormTextField = {
let password = FormTextField()
password.title = "Password"
password.configureCell = { [unowned self] (cell) in
cell.textField.secureTextEntry = true
cell.textField.autocorrectionType = .No
cell.textField.autocapitalizationType = .None
}
return password
}()
}
此示例演示了如何使用 FormKit 创建登录表单。
如您所见,开始构建表单的最容易方法是继承 FormViewController。虽然这并非绝对必要,但强烈推荐这样做。
您第二会注意到的是,FormKit 中的所有表单都是围绕表单模型建立的。表单模型代表每个表单,并由表单部分模型组成。表单部分由表单行类型模型组成,并可选地包含表单部分头部和/或尾部。
一旦配置了表单模型,您只需将其分配给 FormViewController 的表单属性,其余的将由 FormKit 处理。
FormKit 随带多个 FormRowType 模型,可以直接用于创建 iOS 中大多数表单。
一种类型的行,可用于任何不需要用户输入的单元格。此行类型非常适合创建菜单、不需要输入的行或需要完全自定义输入实现的行。
let row = FormRow()
row.icon = UIImage(named: "icon_invitees")
row.title = "Invitees"
row.configureCell = { [unowned self] (cell) in
cell.accessoryType = .DisclosureIndicator
}
row.tap = { [unowned self] (cell) in
self.presentInviteesViewController()
}
一个用于接受通过 UITextField 输入的行。可以很容易地使用提供的配置单元格闭包来配置与单元格关联的 UITextField。
let row = FormTextField()
row.title = "URL"
row.text = self.event.URL?.absoluteString
row.configureCell = { (cell) in
cell.textField.keyboardType = .URL
cell.textField.autocapitalizationType = .None
cell.textField.autocorrectionType = .No
}
row.valueDidChange = { [unowned self] (text) in
guard let text = text else {
self.event.URL = nil
return
}
self.event.URL = NSURL(string: text)
}
一个用于接受通过 UITextView 输入的行。可以很容易地使用提供的配置单元格闭包来配置与单元格关联的 UITextView。
let row = FormTextView()
row.text = self.event.notes
row.configureCell = { (cell) in
cell.textView.editable = false
cell.textView.selectable = true
cell.textView.dataDetectorTypes = .Link
}
row.valueDidChange = { (text) in
self.event.notes = text
}
用于通过UISwitch接受输入的行。
let row = FormSwitch()
row.title = "All Day"
row.on = self.event.day
row.valueDidChange = { [unowned self] (on) in
self.event.day = on
self.configureDateFormatters()
}
允许选择任何类型单选或多选的行。
let row = FormOptionList<Event.Repeat>()
row.title = "Repeat"
row.options = Event.Repeat.all()
row.selection = self.event.repeats
row.stringRepresentatonForOption = { $0.rawValue }
用于从表单日期时间选择器接受日期和/或时间输入的行。此行将在相同的方式下在行下方插入和删除表单日期时间选择器,就像你在日历应用程序的事件表中找到的那样。
let row = FormDateTime(formatter: self.dateTimeFormatter)
row.title = "Start"
row.date = self.event.start
row.valueDidChange = { [unowned self] (date) in
self.event.start = date
}
用于从UIDatePicker接受日期和/或时间输入的行。这将显示具有独立UIDatePicker的行。
let row = FormDateTimePicker()
row.valueDidChange = { [unowned self] (date) in
self.date = date
}
用于显示按钮的行
let row = FormButton()
row.title = "Clear History and Website Data"
row.tap = { [unowned self] (cell) in
self.clear()
}
当前有一种表单部分标题/页脚类型可以用来显示类似于设置应用中显示的部分标题或页脚文本。
let section = FormSection()
section.header = FormSectionHeaderFooter(title: "DEMOS")
Dino Constantinou, [email protected]
FormKit 在MIT许可证下可用。有关更多信息,请参阅LICENSE文件。