Foreword
Eureka
Is a pure Swift
library, it can help you create a fast gracefully TableView
. The library itself offers a number of different functions built Cell
, use is also very simple. If the built-in Cell
style or function can not meet the need, you can also customize Cell
.
Eureka is set using TableView
code something like the following:
form +++ Section()
<<< LabelRow() {
$0.title = "LabelRow"
$0.value = "Default Value"
}.cellSetup({ (cell, row) in
cell.accessoryType = .disclosureIndicator
}).onCellSelection({ (cell, row) in
print("cell selection")
})
This article describes the Eureka
basic usage and custom Cell
steps. You can here see the Demo of this article.
Basic use
Eureka
The basic use of mainly three steps
- 1. Create
ViewController
andViewController
setFormViewController
subclass - 2. Add
Section
- 3. Add
Row
NewViewController
Display tableView
first have a controller, where we create and set it to a controller FormViewController
subclasses, as follows:
class ViewController: FormViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
FormViewController
Itself is a UIViewController
subclass, and Eureka
frame has been added and set up tableView
, so we do not have to worry about the layout of the development, focus only on the interaction logic on it.
Add toSection
FormViewController
In tableView
the style
property value .grouped
, here we add the number Section
will automatically generate the number of groups, add the following syntax:
form +++ Section()
Some people see here perhaps a bit strange, +++
is what grammar, like never seen ah. In fact, this is Eureka
the use of Swift custom operator characteristics, define +++
the infix operator that the source file is so defined:
infix operator +++ : FormPrecedence
Add on top of this Section
syntax is defined like this:
@discardableResult
public func +++ (left: Form, right: Section) -> Form {
left.append(right)
return left
}
Here Form
comply with RangeReplaceableCollection
this agreement, rewrite append
this method realization are interested can go to the source.
Add toRow
Row
Create and set up the core of all the steps, all the interaction logic are done here. Eureka
Built dozens of commonly used Row
, here we add the three most common Row
:
form +++ Section()
<<< LabelRow() {
$0.title = "LabelRow"
$0.value = "Default Value"
}.cellSetup({ (cell, row) in
cell.accessoryType = .disclosureIndicator
}).onCellSelection({ (cell, row) in
print("cell selection")
})
<<< TextRow(tag: "TextRow")
.cellSetup({ (cell, row) in
row.tag = "TextRow"
row.title = "TextRow"
row.placeholder = "Input your message"
})
.cellUpdate({ (cell, row) in
print("cell update")
})
<<< SwitchRow() {
$0.tag = "SwitchValue"
$0.title = "SwitchRow"
$0.value = true
}.onChange({ (row) in
print("value changed: \(row.value ?? false)")
})
<<<
Also is Eureka
the custom infix operator, the role is Row
added to the corresponding Section
in. In the above code, I was in Row
the closure of a method for generating title
and value
assignment, this operation can also be placed cellSetup
(this operation will only method Row
is called when created), the effect is the same. Dynamically refreshed values need to cellUpdate
set the process. Eureka
Also provided onCellSelection
and onChange
this response method, in fact, which defines the logic to respond.
At this point, Eureka
the basic use it all done. If the Eureka
frame comes with Row
has been able to fully meet your needs, you do not look down. But if you are not satisfied with the built-in Row
, so we continue to look at how to customize Cell
andRow
customizeCell
Here we define a simulation software update Cell
, the effect is as follows:
Definition of the structure
First, we need a structure Software
to facilitate the Cell
assignment:
enum UpdateState: Int {
case toUpdate = 0
case updating
case alreadyNewest
}
struct Software: Equatable {
var name: String
var updateState: UpdateState
}
func == (lhs: Software, rhs: Software) -> Bool {
return lhs.name == rhs.name
}
One thing to note here is that class or structure definition must comply with Equatable
the agreement, or in the custom Cell
when in use error.
definitionCell
Defined the structure, the next step is to realize Cell
part.
Custom Eureka
's Cell
and common custom UITableView
of Cell
the same, we must first choose to use pure code created or xib
created. Here we use the xib
method. New Custom Cell
, as shown below:
Custom layout, Cell
wiring, UpdateCell
the code is as follows:
final class UpdateCell: Cell<Software>, CellType {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var updateButton: UIButton!
@IBOutlet weak var aniContainerView: UIView!
@IBOutlet weak var noNeedUpdateLabel: UILabel!
private var shapeLayer = CAShapeLayer()
override func setup() {
selectionStyle = .none
height = { return 44 }
//这里省略布局代码
}
override func update() {
guard let software = row.value else {
return
}
titleLabel.text = software.name
updateUI(software.updateState)
}
}
Creating a UpdateCell
class, there are several points to note:
- Designated as
Cell
the assignment using class - Compliance
CellType
Agreement - Specified
Cell
height
In the above code, the Setup
method will only be Cell
invoked when you create once, where you can write the code layout, and Update
the method will be called each time refresh, where you can give Cell
the subViews
assignment.
definitionRow
Next also need to customize Row
, subordinate to the above, we have just defined Cell
, and loaded inside xib
:
final class UpdateRow: Row<UpdateCell>, RowType {
required init(tag: String?) {
super.init(tag: tag)
cellProvider = CellProvider<UpdateCell>(nibName: "UpdateCell")
}
}
Add custom Cell
to Section
the
Custom Cell
Once created, you can use the framework as that comes Row
, like to use our custom Row
:
<<< UpdateRow() {
$0.value = Software(name: "My Software", updateState: .toUpdate)
}
to sum up
In fact, Eureka
the use of relatively simple, the proficiency will be to quickly build UITableView
a weapon. Demo herein can in here to see.