Shopify / FunctionalTableData

Declarative UITableViewDataSource implementation
MIT License
365 stars 30 forks source link

Row deletions - extra argument in call: rowActions #196

Closed csr closed 4 years ago

csr commented 4 years ago

Hi, I previously used to delete rows by specifying a UITableViewRowAction in rowActions property like so

let deleteRowAction = UITableViewRowAction(style: .destructive, title: "Delete", handler: ({ (rowAction, indexPath) in
    self.didTapDeleteOnRow(indexPath: indexPath)
}))

let rows: [CellConfigType] = getSourceArray().enumerated().map { index, item in
    return HistoryCell(
        key: "index-\(index)-\(item.translatedText)",
        style: cellStyle,
        actions: CellActions(
            selectionAction: { _ in
                self.didSelectCell(translation: item)
                return .selected
        },
            deselectionAction: { _ in
                return .deselected
        }, rowActions: [deleteRowAction]),   // <---- here
        state: HistoryState(translationItem: item),
        cellUpdater: HistoryState.updateView)
}

Now I see rowActions isn't part of the initializer anymore, so I'm wondering how to enable cell deletion. Any insights?

g-Off commented 4 years ago

Hi @csr, thanks for reaching out. We've moved to a slightly different model here to better take advantage of the APIs exposed in iOS 11+, specifically the UISwipeActionsConfiguration. This allows images and full row swipe as well as having both leading and trailing actions.

I've tweaked your example a bit to show how the new version would work. One note is that I had to change the call to self.didTapDeleteOnRow to self.didTapDeleteOnItem since an IndexPath isn't available anymore.

let rows: [CellConfigType] = getSourceArray().enumerated().map { index, item in
    let deleteAction = CellActions.SwipeActionsConfiguration.ContextualAction(title: "Delete", backgroundColor: UIColor.red, style: .destructive) { _, completion in
        self.didTapDeleteOnItem(item)
        completion(true) // The true signifies a successful deletion
    }
    return HistoryCell(
        key: "index-\(index)-\(item.translatedText)",
        style: cellStyle,
        actions: CellActions(
            selectionAction: { _ in
                self.didSelectCell(translation: item)
                return .selected
        },
            deselectionAction: { _ in
                return .deselected
        },
            trailingActionConfiguration: CellActions.SwipeActionsConfiguration(actions: [deleteAction]) // <---- here
        ),
        state: HistoryState(translationItem: item),
        cellUpdater: HistoryState.updateView)
}