MultivaluedSection的奇怪行为?

时间:2018-03-14 06:11:39

标签: ios eureka-forms

我一直在使用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
    }
}

运行应用,如下图所示:

enter image description here

有两个问题:

1:你可以看到我添加3个人然后按顺序删除它们,一切都很好!但是,当我再次添加一个人时,发生了一些错误:似乎该部分的标题被拉得很长。为什么???

2:当我向tableView添加一些人时,标题没有左对齐,为什么会这样:

enter image description here

非常感谢!

2 个答案:

答案 0 :(得分:1)

请更新代码,

peopleSection.multivaluedRowToInsertAt = {idx in
    return LabelRow() {
        let person = Person(name: "h\(idx)")
        $0.title = person.description
        self.people.append(person)
    }
}

它将为您提供以下输出,删除也将正常工作。

enter image description here

答案 1 :(得分:0)

关于您的第一个问题:SwipeAction实际上并未从您的表中删除该行。为了使其正常工作,您可以手动删除此行:

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]