Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for table row deletion #124

Closed
csr opened this issue Dec 12, 2018 · 2 comments
Closed

Adding support for table row deletion #124

csr opened this issue Dec 12, 2018 · 2 comments

Comments

@csr
Copy link

csr commented Dec 12, 2018

Hi, I would like to understand how to correctly handle table view row deletions with FunctionalTableData. I understand that we should not work with indexes, however right now this is what I came up with:

class MyViewController: UITableViewController {
    internal let functionalData = FunctionalTableData()
    
    internal var items: [Translation] = [] {
        didSet {
            render()
        }
    }
    
    private func render() {
        let cellStyle = CellStyle(bottomSeparator: .inset, separatorColor: .gray, backgroundColor: .black)
        let rows: [CellConfigType] = items.enumerated().map { index, item in
            return HistoryCell(
                key: "id-\(index)",
                style: cellStyle,
                actions: CellActions(rowActions: [UITableViewRowAction(style: .destructive, title: "Delete", handler: ({ (rowAction, indexPath) in
                    self.items.remove(at: indexPath.row)
                }))]),
                state: HistoryState(translationItem: item),
                cellUpdater: HistoryState.updateView)
        }
        
        functionalData.renderAndDiff([
            TableSection(key: "section", rows: rows)
            ])
    }

    ...
}

My intuition was that I needed to create a new CellAction, however, I'm not entirely sure how to handle the data deletion from the array (it's not working well, more specifically when I try to delete any row the last row gets deleted). I would appreciate any help with this, thanks!

@raulriera
Copy link
Contributor

Hi, you mentioned the solution in the beginning 🙂

I understand that we should not work with indexes

Given that the library checks for the key first and then the equality of the states in all cells, by making your cells using keys of numbers; Will make it that when you delete an item, all of them will be different given that their key becomes pretty much this id-\(index-1).

key: "id-\(index)",

I would suggest keeping the same code you have here but change the key to something meaningful and unique to your Translation, like key: item.id, for example

@csr
Copy link
Author

csr commented Dec 12, 2018

Sweet thanks!! :)

@csr csr closed this as completed Dec 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants