MultivaluedSection 的奇怪行为?
Strange behavior with MultivaluedSection?
)
我用Eureka有一段时间了,太神奇了!!!
最近我在研究 MultivaluedSection,我写了一个简单的测试项目:它很简单 add/delete 来自 tableView 的人。
这里是代码,首先是模型:人物
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
}
}
运行 app,如下图:
有2个问题:
1:你可以看到我加了3个人,然后按顺序删除,一切正常!但是当我再次添加人员时,发生了一些错误:似乎该部分的 header 被拉得很长。为什么???
2:我在tableView中添加人物时,标题不是left-aligned,这是为什么:
非常感谢!
请更新代码,
peopleSection.multivaluedRowToInsertAt = {idx in
return LabelRow() {
let person = Person(name: "h\(idx)")
[=10=].title = person.description
self.people.append(person)
}
}
它会给你下面的输出,删除也能正常工作。
关于您的第一个问题:SwipeAction 实际上并没有从您的 table 中删除该行。为了使其正常工作,您可以像这样手动删除此行:
let deleteAction = SwipeAction(style: .destructive, title: "DEL") { action, row, completion in
// Delete row:
if let rowNum = row.indexPath?.row {
row.section?.remove(at: rowNum)
}
completion?(true)
}
row.trailingSwipe.actions = [deleteAction]
)
我用Eureka有一段时间了,太神奇了!!!
最近我在研究 MultivaluedSection,我写了一个简单的测试项目:它很简单 add/delete 来自 tableView 的人。
这里是代码,首先是模型:人物
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
}
}
运行 app,如下图:
有2个问题:
1:你可以看到我加了3个人,然后按顺序删除,一切正常!但是当我再次添加人员时,发生了一些错误:似乎该部分的 header 被拉得很长。为什么???
2:我在tableView中添加人物时,标题不是left-aligned,这是为什么:
非常感谢!
请更新代码,
peopleSection.multivaluedRowToInsertAt = {idx in
return LabelRow() {
let person = Person(name: "h\(idx)")
[=10=].title = person.description
self.people.append(person)
}
}
它会给你下面的输出,删除也能正常工作。
关于您的第一个问题:SwipeAction 实际上并没有从您的 table 中删除该行。为了使其正常工作,您可以像这样手动删除此行:
let deleteAction = SwipeAction(style: .destructive, title: "DEL") { action, row, completion in
// Delete row:
if let rowNum = row.indexPath?.row {
row.section?.remove(at: rowNum)
}
completion?(true)
}
row.trailingSwipe.actions = [deleteAction]