MultivaluedSection is a class in Eureka created to support section with many child values in which user can CRUD its data. In this post you’ll be guided to custom add button of MultivaluedSection.
First, create a class inherits from ButtonCell such as:
class MyButtonCell: ButtonCell {
override func update() {
super.update()
selectionStyle = .none
textLabel?.isHidden = true
}
}
Then when create Multi config add button like this:
section.addButtonProvider = { _ in
return ButtonRow("AddButtonRow") { row in
row.title = "Add New Tag"
row.cellProvider = CellProvider<ButtonCell>(nibName: "MyButtonCell", bundle: Bundle.main)
}
}
Since addButtonProvider closure return AddButtonType which is RowType, therefore basically we can return any type that adapts RowType in this closure.
If you want to prevent Eureka’s default add action, such as you have a custom button in MyButtonCell class with IBAction and you want to trigger add action inside that IBAction only then you can call row.didSelect() method like this:
class MyButtonCell: ButtonCell {
// ...
@IBAction func buttonClicked(_ sender: Any) {
row.didSelect()
}
}
Then in UIViewController add override method func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? to prevent Eureka’s default add action:
override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
let row = form.allSections[indexPath.section].allRows[indexPath.row]
if row.tag == "AddButtonRow" {
return nil
}
return indexPath
}
That’s it, here is the link of the demo source code. You download and check for more detail. Have a nice day!
