4

)

I have been using Eureka for a while,It's amazing!!!

Recently I work on with MultivaluedSection,I write a simple project for test : It simple add/delete person from tableView.

here are code , first for model : 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
    }
}

next code for 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
    }
}

Run app , like the following picture:

enter image description here

There are 2 questions :

1:You can see when I add 3 person then delete them in order , everything is fine! But when I add person again some wrong is happened : it seem like the section's header was pulled very long. why???

2:When I added some people to tableView,there're title are not left-aligned,Why is that :

enter image description here

Thanks a lot!

Cœur
  • 37,241
  • 25
  • 195
  • 267
hopy
  • 559
  • 3
  • 18

2 Answers2

1

Please update the code,

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

It will give you following output, and delete will also work properly.

enter image description here

PPL
  • 6,357
  • 1
  • 11
  • 30
0

About your 1st question: the SwipeAction doesn't actually delete the row from your table. To make it work properly you can manually delete this row like this:

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]
Tonnie
  • 149
  • 2
  • 8