在我點擊警報操作后,我需要洗掉單元格,但在警報控制器出現之前洗掉了單元格。
@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)
}
在我點擊警報操作后,我需要洗掉單元格,但在警報控制器出現之前洗掉了單元格。
uj5u.com熱心網友回復:
您忽略了handler用戶按下按鈕時呼叫的操作引數。
您的大部分代碼都是多余的,您可以通過向后洗掉專案來從陣列中洗掉多個索引處的專案。
usersTableView.begin/endUpdates()對單個洗掉操作無效。
并且關閉線是毫無意義的,因為警報控制器在按下按鈕時會隱式關閉視圖
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")
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/526002.html
標籤:IOS迅速
上一篇:代碼生成
