Home > Back-end >  I need delete cell after i tapped on alert action, but cells deleted before alert controller appear
I need delete cell after i tapped on alert action, but cells deleted before alert controller appear

Time:11-03

I need delete cell after i tapped on alert action, but cells deleted before alert controller appear.

 @IBAction func binButton(_ sender: Any) {
        
        let subtitleString = NSAttributedString(string: "Are you sure to delete?", attributes: [
            NSAttributedString.Key.font : UIFont(name: "SFProText-Regular", size: 13) ?? .systemFont(ofSize: 13),
            NSAttributedString.Key.foregroundColor: UIColor.white
        ])
        
        let alert = UIAlertController(title: "Take Attention", message: "", preferredStyle: .alert)
        alert.setValue(subtitleString, forKey: "attributedMessage")
        alert.view.subviews.first?.subviews.first?.subviews.first?.backgroundColor = UIColor(named: "AlertColor")
        alert.view.tintColor = UIColor.white
        let cancelAlert = UIAlertAction(title: "No", style: .default, handler: nil)
        cancelAlert.setValue(UIColor(named: "Orange"), forKey: "titleTextColor")
        
        
        
        let actionDelete = UIAlertAction(title: "Yes", style: .default, handler: nil)
        actionDelete.setValue(UIColor(named: "companyTextColor"), forKey: "titleTextColor")
        alert.dismiss(animated: true, completion: nil)
        if let selectedRows = usersTableView.indexPathsForSelectedRows {
            // 1
            var items = [String]()
            for indexPath in selectedRows  {
                items.append(userName[indexPath.row])
            }
            // 2
            for item in items {
                if let index = userName.firstIndex(of: item) {
                    userName.remove(at: index)
                }
            }
            // 3
            usersTableView.beginUpdates()
            usersTableView.deleteRows(at: selectedRows, with: .automatic)
            usersTableView.endUpdates()
        }
    
            alert.addAction(cancelAlert)
            alert.addAction(actionDelete)
            self.present(alert, animated: true, completion: nil)
        }

I need delete cell after i tapped on alert action, but cells deleted before alert controller appear.

CodePudding user response:

You are ignoring the handler parameter of the action which is called when the user presses the button.

Most of your code is redundant, you can remove items at multiple indices from an array by removing the items backwards.

usersTableView.begin/endUpdates() has no effect for a single delete operation.

And the dismiss line is pointless as the alert controller dismisses the view implicitly when a button is pressed

let actionDelete = UIAlertAction(title: "Yes", style: .default) { _ in
    if let selectedRows = usersTableView.indexPathsForSelectedRows {
        for indexPath in selectedRows.reversed()  {
            userName.remove(at: indexPath.row)
        }
        usersTableView.deleteRows(at: selectedRows, with: .automatic)
    }

}
actionDelete.setValue(UIColor(named: "companyTextColor"), forKey: "titleTextColor")
  • Related