)
我已经使用Eureka一段时间了,太棒了!
最近我在使用MultivaluedSection,我编写了一个简单的测试项目:它简单地从tableView添加/删除人.
这里是代码,首先是模型:Person
struct Person:Equatable,CustomStringConvertible{
var description: String{
return "\(name) \(id)"
}
static func ==(lhs: Person, rhs: Person) -> Bool {
return lhs.id == rhs.id
}
var id:String
var name:String
init(name:String){
self.id = UUID().uuidString
self.name = name
}
}
VC的下一个代码:
class ViewController: FormViewController {
var people:[Person] = []
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//hide delete button at row left
tableView.isEditing = false
}
override func viewDidLoad() {
super.viewDidLoad()
let peopleSection = MultivaluedSection(multivaluedOptions:[.Delete,.Reorder,.Insert],header:"people")
peopleSection.tag = "people"
peopleSection.multivaluedRowToInsertAt = {idx in
let newRow = LabelRow(){row in
let person = Person(name: "h\(idx)")
row.value = person.description
self.people.append(person)
let deleteAction = SwipeAction(style: .destructive, title: "DEL"){action,row,completion in
completion?(true)
}
row.trailingSwipe.actions = [deleteAction]
}
return newRow
}
peopleSection.addButtonProvider = {section in
let addBtn = ButtonRow("add"){row in
row.title = "new person"
}
return addBtn
}
form +++ peopleSection
}
}
运行应用程序,如下图所示:
有两个问题:
1:你可以看到我添加3个人然后按顺序删除它们,一切都很好!但是,当我再次添加一个人时,发生了一些错误:似乎该部分的标题被拉得很长.为什么???
2:当我向tableView添加一些人时,标题没有左对齐,为什么:
非常感谢!
最佳答案 请更新代码,
peopleSection.multivaluedRowToInsertAt = {idx in
return LabelRow() {
let person = Person(name: "h\(idx)")
$0.title = person.description
self.people.append(person)
}
}
它将为您提供以下输出,删除也将正常工作.