user10461683
user10461683

Reputation:

Is there a way to know when a UITableViewCell is removed from UITableView?

I am showing a UITableView which is driven by RxRealmDataSources.

I need to perform some actions when a row in the table gets deleted.

Is there a way such that whenever a row gets deleted from the table, a function gets called with the indexpath of the deleted row?

Edit -

The UI of a cell of the UITableView in my app depends on 2 things -

Whenever, a cell gets deleted, I need to update the UI of its next cell.

If the only way the db ever got updated was by the direct action of the user, then I could have used func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) method to get the indexPath of the cell that should be deleted and update the UI of the next cell.

However, the db is synced to cloud and the db is binded to the table view so that I do not have control on when cells gets added or deleted. It is for this reason, I wanted to know if there is a way to know when a cell is removed from UITableView

Upvotes: 0

Views: 1669

Answers (4)

Peng in
Peng in

Reputation: 119

You have to implement 1 delegate method of UITableView.

  • trailingSwipeActionsConfigurationForRowAt

It's easy to implement. This delegate method will be called twice, one when you swipe and again when you press to delete a row.

`enter code here`
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        
        let config =  UISwipeActionsConfiguration(actions: [makeDeleteContextualAction(forRowAt: indexPath)])
        config.performsFirstActionWithFullSwipe = true
        
        return config
    }
    
    private func makeDeleteContextualAction(forRowAt indexpath:IndexPath) -> UIContextualAction {
        
        let deleteAction = UIContextualAction(style: .destructive, title: LocalizableConstants.constLS_global_delete()) { (action, swipeButtonView, completion) in
    
            let product = self.products[indexpath.row]
            if let quantity = product.vo_quantity(), let amount = product.vo_priceFide() {
                self.totalProducts -= Int(truncating: quantity)
                self.totalAmount -= amount.doubleValue * quantity.doubleValue
            }
            
            DispatchQueue.main.async {
                self.lbBasketNumber.text = String(self.totalProducts)
                self.lbTotalAmount.text = String(self.totalAmount)
            }
            
            self.products.remove(at: indexpath.row)
            self.tableView.deleteRows(at: [indexpath], with: .fade)
            
            if #available(iOS 13.0, *) {
                action.image = ImagesConstants.constIMG_XCA_mini_icon_red_trash()
                action.image?.withTintColor(ConstantsColor.const_COLOR_RED())
                action.backgroundColor = ConstantsColor.const_COLOR_WHITE()
            } else {
                action.title = LocalizableConstants.constLS_global_delete()
            }
            
            completion(true)
        }
        
        return deleteAction
    }

Upvotes: 0

Daniel T.
Daniel T.

Reputation: 33967

In a comment you said:

By 'deleting', I mean when the cell is removed from the tableview like when we swipe the cell to the left to remove it.

Since you tagged RxSwift, the solution is to use itemDeleted as in:

tableView.rx.itemDeleted
    .subscribe(onNext: { print("delete item at index path \($0) from your model.")})
    .disposed(by: bag)

If you aren't looking for an Rx solution, then your question is a dup of:

Add swipe to delete UITableViewCell

Upvotes: 1

user10461683
user10461683

Reputation:

I was able to solve this by subclassing the UITableView class and overriding the func deleteRows(at indexPaths: [IndexPath], with animation: UITableView.RowAnimation) method.

Upvotes: 0

Ievgen Leichenko
Ievgen Leichenko

Reputation: 1317

Due to the reusability of cells in UITableView, cells are not actually deleted until the table itself is deallocated.

I might assume that by 'deleting' cell you mean cell disappearing from the screen. In this case the following function of UITableViewDelegate might help you (called when the cell is not visible any more):

func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath)

Upvotes: 1

Related Questions