Eureka

Eureka

Swift表单构建框架简化iOS开发

Eureka是一个Swift编写的iOS表单构建框架,提供丰富的表单控件和功能。它支持动态显示/隐藏、自定义行类型、验证、多值选择等特性。Eureka使用简单,几行代码即可创建复杂表单,同时保持高度可定制性。框架适用于各种复杂程度的表单需求,能有效提升iOS开发效率。

EurekaSwift表单构建iOS开发自定义行Github开源项目

Eureka: Elegant form builder in Swift

<p align="center"> <a href="https://travis-ci.org/xmartlabs/Eureka"><img src="https://travis-ci.org/xmartlabs/Eureka.svg?branch=master" alt="Build status" /></a> <img src="https://img.shields.io/badge/platform-iOS-blue.svg?style=flat" alt="Platform iOS" /> <a href="https://developer.apple.com/swift"><img src="https://img.shields.io/badge/swift5-compatible-4BC51D.svg?style=flat" alt="Swift 5 compatible" /></a> <a href="https://github.com/Carthage/Carthage"><img src="https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat" alt="Carthage compatible" /></a> <a href="https://cocoapods.org/pods/Eureka"><img src="https://img.shields.io/cocoapods/v/Eureka.svg" alt="CocoaPods compatible" /></a> <a href="https://raw.githubusercontent.com/xmartlabs/Eureka/master/LICENSE"><img src="http://img.shields.io/badge/license-MIT-blue.svg?style=flat" alt="License: MIT" /></a> <a href="https://codebeat.co/projects/github-com-xmartlabs-eureka"><img alt="codebeat badge" src="https://codebeat.co/badges/16f29afb-f072-4633-9497-333c6eb71263" /></a> </p>

Made with ❤️ by XMARTLABS. This is the re-creation of [XLForm] in Swift.

简体中文

Overview

<table> <tr> <th> <img src="Example/Media/EurekaExample1.gif" width="220"/> </th> <th> <img src="Example/Media/EurekaExample2.gif" width="220"/> </th> <th> <img src="Example/Media/EurekaExample3.gif" width="220"/> </th> </tr> </table>

Contents

  • [Requirements]
  • [Usage]
    • [How to create a Form]
    • [Getting row values]
    • [Operators]
    • [Using the callbacks]
    • [Section Header and Footer]
    • [Dynamically hide and show rows (or sections)]
    • [List sections]
    • [Multivalued sections]
    • [Validations]
    • [Swipe Actions]
  • [Custom rows]
    • [Basic custom rows]
    • [Custom inline rows]
    • [Custom presenter rows]
  • [Row catalog]
  • [Installation]
  • [FAQ]

For more information look at [our blog post] that introduces Eureka.

Requirements (for latest release)

  • Xcode 11+
  • Swift 5.0+

Example project

You can clone and run the Example project to see examples of most of Eureka's features.

<table> <tr> <th> <img src="Example/Media/EurekaNavigation.gif" width="200"/> </th> <th> <img src="Example/Media/EurekaRows.gif" width="200"/> </th> </tr> </table>

Usage

How to create a form

By extending FormViewController you can then simply add sections and rows to the form variable.

import Eureka class MyFormViewController: FormViewController { override func viewDidLoad() { super.viewDidLoad() form +++ Section("Section1") <<< TextRow(){ row in row.title = "Text Row" row.placeholder = "Enter text here" } <<< PhoneRow(){ $0.title = "Phone Row" $0.placeholder = "And numbers here" } +++ Section("Section2") <<< DateRow(){ $0.title = "Date Row" $0.value = Date(timeIntervalSinceReferenceDate: 0) } } }

In the example we create two sections with standard rows, the result is this:

<center> <img src="Example/Media/EurekaHowTo.gif" width="200" alt="Screenshot of Custom Cells"/> </center>

You could create a form by just setting up the form property by yourself without extending from FormViewController but this method is typically more convenient.

Configuring the keyboard navigation accesory

To change the behaviour of this you should set the navigation options of your controller. The FormViewController has a navigationOptions variable which is an enum and can have one or more of the following values:

  • disabled: no view at all
  • enabled: enable view at the bottom
  • stopDisabledRow: if the navigation should stop when the next row is disabled
  • skipCanNotBecomeFirstResponderRow: if the navigation should skip the rows that return false to canBecomeFirstResponder()

The default value is enabled & skipCanNotBecomeFirstResponderRow

To enable smooth scrolling to off-screen rows, enable it via the animateScroll property. By default, the FormViewController jumps immediately between rows when the user hits the next or previous buttons in the keyboard navigation accesory, including when the next row is off screen.

To set the amount of space between the keyboard and the highlighted row following a navigation event, set the rowKeyboardSpacing property. By default, when the form scrolls to an offscreen view no space will be left between the top of the keyboard and the bottom of the row.

class MyFormViewController: FormViewController { override func viewDidLoad() { super.viewDidLoad() form = ... // Enables the navigation accessory and stops navigation when a disabled row is encountered navigationOptions = RowNavigationOptions.Enabled.union(.StopDisabledRow) // Enables smooth scrolling on navigation to off-screen rows animateScroll = true // Leaves 20pt of space between the keyboard and the highlighted row after scrolling to an off screen row rowKeyboardSpacing = 20 } }

If you want to change the whole navigation accessory view, you will have to override the navigationAccessoryView variable in your subclass of FormViewController.

Getting row values

The Row object holds a value of a specific type. For example, a SwitchRow holds a Bool value, while a TextRow holds a String value.

// Get the value of a single row let row: TextRow? = form.rowBy(tag: "MyRowTag") let value = row.value // Get the value of all rows which have a Tag assigned // The dictionary contains the 'rowTag':value pairs. let valuesDictionary = form.values()

Operators

Eureka includes custom operators to make form creation easy:

+++       Add a section

form +++ Section() // Chain it to add multiple Sections form +++ Section("First Section") +++ Section("Another Section") // Or use it with rows and get a blank section for free form +++ TextRow() +++ TextRow() // Each row will be on a separate section

<<<       Insert a row

form +++ Section() <<< TextRow() <<< DateRow() // Or implicitly create the Section form +++ TextRow() <<< DateRow()

+=        Append an array

// Append Sections into a Form form += [Section("A"), Section("B"), Section("C")] // Append Rows into a Section section += [TextRow(), DateRow()]

Result builders

Eureka includes result builders to make form creation easy:

@SectionBuilder

// Section + Section form = (Section("A") +++ { URLRow("UrlRow_f1") { $0.title = "Url" } if something { TwitterRow("TwitterRow_f2") { $0.title = "Twitter" } } else { TwitterRow("TwitterRow_f1") { $0.title = "Twitter" } } AccountRow("AccountRow_f1") { $0.title = "Account" } }) // Form + Section form +++ { if something { PhoneRow("PhoneRow_f1") { $0.title = "Phone" } } else { PhoneRow("PhoneRow_f2") { $0.title = "Phone" } } PasswordRow("PasswordRow_f1") { $0.title = "Password" } }

@FormBuilder

@FormBuilder var form: Form { Section("Section A") { section in section.tag = "Section_A" } if true { Section("Section B") { section in section.tag = "Section_B" } } NameRow("NameRow_f1") { $0.title = "Name" } }

Using the callbacks

Eureka includes callbacks to change the appearance and behavior of a row.

Understanding Row and Cell

A Row is an abstraction Eureka uses which holds a value and contains the view Cell. The Cell manages the view and subclasses UITableViewCell.

Here is an example:

let row = SwitchRow("SwitchRow") { row in // initializer row.title = "The title" }.onChange { row in row.title = (row.value ?? false) ? "The title expands when on" : "The title" row.updateCell() }.cellSetup { cell, row in cell.backgroundColor = .lightGray }.cellUpdate { cell, row in cell.textLabel?.font = .italicSystemFont(ofSize: 18.0) }
<img src="Example/Media/EurekaOnChange.gif" width="300" alt="Screenshot of Disabled Row"/>

Callbacks list

  • onChange()

    Called when the value of a row changes. You might be interested in adjusting some parameters here or even make some other rows appear or disappear.

  • onCellSelection()

    Called each time the user taps on the row and it gets selected. Note that this will also get called for disabled rows so you should start your code inside this callback with something like guard !row.isDisabled else { return }

  • cellSetup()

    Called only once when the cell is first configured. Set permanent settings here.

  • cellUpdate()

    Called each time the cell appears on screen. You can change the appearance here using variables that may not be present on cellSetup().

  • onCellHighlightChanged()

    Called whenever the cell or any subview become or resign the first responder.

  • onRowValidationChanged()

    Called whenever the the validation errors associated with a row changes.

  • onExpandInlineRow()

    Called before expanding the inline row. Applies to rows conforming InlineRowType protocol.

  • onCollapseInlineRow()

    Called before collapsing the inline row. Applies to rows conforming InlineRowType protocol.

  • onPresent()

    Called by a row just before presenting another view controller. Applies to rows conforming PresenterRowType protocol. Use it to set up the presented controller.

Section Header and Footer

You can set a title String or a custom View as the header or footer of a Section.

String title

Section("Title") Section(header: "Title", footer: "Footer Title") Section(footer: "Footer Title")

Custom view

You can use a Custom View from a .xib file:

Section() { section in var header = HeaderFooterView<MyHeaderNibFile>(.nibFile(name: "MyHeaderNibFile", bundle: nil)) // Will be called every time the header appears on screen header.onSetupView = { view, _ in // Commonly used to setup texts inside the view // Don't change the view hierarchy or size here! } section.header = header }

Or a custom UIView created programmatically

Section(){ section in var header = HeaderFooterView<MyCustomUIView>(.class) header.height = {100} header.onSetupView = { view, _ in view.backgroundColor = .red } section.header = header }

Or just build the view with a Callback

Section(){ section in section.header = { var header = HeaderFooterView<UIView>(.callback({ let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) view.backgroundColor = .red return view })) header.height = { 100 } return header }() }

Dynamically hide and show rows (or sections) <a name="hide-show-rows"></a>

<img src="Example/Media/EurekaSwitchSections.gif" width="300" alt="Screenshot of Hidden Rows" />

In this case we are hiding and showing whole sections.

To accomplish this each row has a hidden variable of optional type Condition which can be set using a function or NSPredicate.

Hiding using a function condition

Using the function case of Condition:

Condition.function([String], (Form)->Bool)

The array of String to pass should contain the tags of the rows this row depends on. Each time the value of any of those rows changes the function is reevaluated. The function then takes the Form and returns a Bool indicating whether the row should be hidden or not. This the most powerful way of setting up the hidden property as it has no explicit limitations of what can be done.

form +++ Section() <<< SwitchRow("switchRowTag"){ $0.title = "Show message" } <<< LabelRow(){ $0.hidden = Condition.function(["switchRowTag"], { form in return !((form.rowBy(tag: "switchRowTag") as? SwitchRow)?.value ?? false) }) $0.title = "Switch is on!" }
<img src="Example/Media/EurekaHidden.gif" width="300" alt="Screenshot of Hidden Rows" />
public enum Condition { case function([String], (Form)->Bool) case predicate(NSPredicate) }

Hiding using an NSPredicate

The hidden variable can also be set with a NSPredicate. In the predicate string you can reference values of other rows by their tags to determine if a row should be hidden or visible. This will only work if the values of the rows the predicate has to check are NSObjects (String and Int will work as they are bridged to their ObjC counterparts, but enums won't work). Why could it then be useful to use predicates when they are more limited? Well, they can be much simpler, shorter and readable than functions. Look at this example:

$0.hidden = Condition.predicate(NSPredicate(format: "$switchTag == false"))

And we can write it even shorter since Condition conforms to ExpressibleByStringLiteral:

$0.hidden = "$switchTag == false"

Note: we will substitute the value of the row whose tag is 'switchTag' instead of '$switchTag'

For all of this to work, all of the implicated rows must have a tag as the tag will identify them.

We can also hide a row by doing:

$0.hidden = true

as Condition conforms to ExpressibleByBooleanLiteral.

Not setting the hidden variable will leave the row always visible.

If you manually set the hidden (or disabled) condition after the form has been displayed you may have to call row.evaluateHidden() to force Eureka to reevaluate the new condition. See this FAQ section for more info.

Sections

For sections this works just the same. That means we can set up section hidden property to show/hide it dynamically.

Disabling rows

To disable rows, each row has an disabled variable which is also an optional Condition type property. This variable also works the same as the hidden variable so that it requires the rows to have a tag.

Note that if you want to disable a row permanently you can also set disabled variable to true.

List Sections

To display a list of options, Eureka includes a special section called SelectableSection. When creating one you need to pass the type of row to use in the options and the selectionType. The selectionType is an enum which can be either multipleSelection or singleSelection(enableDeselection: Bool) where the enableDeselection parameter determines if the selected rows can be deselected or not.

form +++ SelectableSection<ListCheckRow<String>>("Where do you live", selectionType: .singleSelection(enableDeselection: true)) let continents = ["Africa", "Antarctica", "Asia", "Australia", "Europe", "North America", "South America"] for option in continents { form.last! <<< ListCheckRow<String>(option){ listRow in listRow.title = option listRow.selectableValue = option listRow.value = nil } }
What kind of rows can be used?

To create such a section you have to create a row that conforms the SelectableRowType protocol.

public protocol SelectableRowType : RowType { var selectableValue : Value? { get set } }

This selectableValue is where the value of the row will be permanently stored. The value variable will be used to determine if the row is selected or not, being 'selectableValue' if selected or nil otherwise. Eureka includes the ListCheckRow which is used for example. In the custom rows of the Examples project you can also find the ImageCheckRow.

Getting the selected rows

To easily get the selected row/s of a SelectableSection there are two methods: selectedRow() and selectedRows() which can be called to get the selected row in case it is a SingleSelection section or all the selected rows if it is a MultipleSelection section.

Grouping options in sections

Additionally you can setup list of options to

编辑推荐精选

Trae

Trae

字节跳动发布的AI编程神器IDE

Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。

AI工具TraeAI IDE协作生产力转型热门
蛙蛙写作

蛙蛙写作

AI小说写作助手,一站式润色、改写、扩写

蛙蛙写作—国内先进的AI写作平台,涵盖小说、学术、社交媒体等多场景。提供续写、改写、润色等功能,助力创作者高效优化写作流程。界面简洁,功能全面,适合各类写作者提升内容品质和工作效率。

AI辅助写作AI工具蛙蛙写作AI写作工具学术助手办公助手营销助手AI助手
问小白

问小白

全能AI智能助手,随时解答生活与工作的多样问题

问小白,由元石科技研发的AI智能助手,快速准确地解答各种生活和工作问题,包括但不限于搜索、规划和社交互动,帮助用户在日常生活中提高效率,轻松管理个人事务。

热门AI助手AI对话AI工具聊天机器人
Transly

Transly

实时语音翻译/同声传译工具

Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。

讯飞智文

讯飞智文

一键生成PPT和Word,让学习生活更轻松

讯飞智文是一个利用 AI 技术的项目,能够帮助用户生成 PPT 以及各类文档。无论是商业领域的市场分析报告、年度目标制定,还是学生群体的职业生涯规划、实习避坑指南,亦或是活动策划、旅游攻略等内容,它都能提供支持,帮助用户精准表达,轻松呈现各种信息。

AI办公办公工具AI工具讯飞智文AI在线生成PPTAI撰写助手多语种文档生成AI自动配图热门
讯飞星火

讯飞星火

深度推理能力全新升级,全面对标OpenAI o1

科大讯飞的星火大模型,支持语言理解、知识问答和文本创作等多功能,适用于多种文件和业务场景,提升办公和日常生活的效率。讯飞星火是一个提供丰富智能服务的平台,涵盖科技资讯、图像创作、写作辅助、编程解答、科研文献解读等功能,能为不同需求的用户提供便捷高效的帮助,助力用户轻松获取信息、解决问题,满足多样化使用场景。

热门AI开发模型训练AI工具讯飞星火大模型智能问答内容创作多语种支持智慧生活
Spark-TTS

Spark-TTS

一种基于大语言模型的高效单流解耦语音令牌文本到语音合成模型

Spark-TTS 是一个基于 PyTorch 的开源文本到语音合成项目,由多个知名机构联合参与。该项目提供了高效的 LLM(大语言模型)驱动的语音合成方案,支持语音克隆和语音创建功能,可通过命令行界面(CLI)和 Web UI 两种方式使用。用户可以根据需求调整语音的性别、音高、速度等参数,生成高质量的语音。该项目适用于多种场景,如有声读物制作、智能语音助手开发等。

咔片PPT

咔片PPT

AI助力,做PPT更简单!

咔片是一款轻量化在线演示设计工具,借助 AI 技术,实现从内容生成到智能设计的一站式 PPT 制作服务。支持多种文档格式导入生成 PPT,提供海量模板、智能美化、素材替换等功能,适用于销售、教师、学生等各类人群,能高效制作出高品质 PPT,满足不同场景演示需求。

讯飞绘文

讯飞绘文

选题、配图、成文,一站式创作,让内容运营更高效

讯飞绘文,一个AI集成平台,支持写作、选题、配图、排版和发布。高效生成适用于各类媒体的定制内容,加速品牌传播,提升内容营销效果。

热门AI辅助写作AI工具讯飞绘文内容运营AI创作个性化文章多平台分发AI助手
材料星

材料星

专业的AI公文写作平台,公文写作神器

AI 材料星,专业的 AI 公文写作辅助平台,为体制内工作人员提供高效的公文写作解决方案。拥有海量公文文库、9 大核心 AI 功能,支持 30 + 文稿类型生成,助力快速完成领导讲话、工作总结、述职报告等材料,提升办公效率,是体制打工人的得力写作神器。

下拉加载更多