我正在使用Swift 3.1,我的一个viewcontrollers中有一个表。当用户通过向左滑动并按下红色删除按钮删除表中的一个项目时,我想要弹出警报消息并说出一些内容。当我向左滑动以从UITableView表中删除某些内容时,下面的代码由于某种原因没有被触发。请参阅下面的10行左右,我用“CODE HERE”标记我的尝试。有什么方法可以在删除某些内容时触发此代码吗?
import UIKit
class CheckoutViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var table: UITableView!
@IBOutlet weak var checkoutButton: UIButton!
@IBOutlet weak var priceLabel: UILabel!
var items: [String] = []
internal func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
// CODE HERE
func table(table: UITableView!, canEditRowAtIndexPath indexPath: NSIndexPath!) -> Bool {
return true
}
func table(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
let alertController = UIAlertController(title: "Sample Title", message: "Sample message", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "Ok", style: .default, handler: nil)
alertController.addAction(defaultAction)
present(alertController, animated: true, completion: nil)
}
// CODE HERE
internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")
cell.textLabel?.numberOfLines = 16
cell.textLabel?.font = cell.textLabel?.font.withSize(10)
cell.textLabel?.text = items[indexPath.row]
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(_ animated: Bool) {
priceLabel.text = String(format: "%.2f", totalPrice)
let itemsObject = UserDefaults.standard.object(forKey: "items")
if let tempItems = itemsObject as? [String] {
items = tempItems
}
table.reloadData()
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete {
items.remove(at: indexPath.row)
table.reloadData()
UserDefaults.standard.set(items, forKey: "items")
}
}
@IBAction func checkoutClicked(_ sender: Any) {
priceLabel.text = String(format: "%.2f", totalPrice)
items.removeAll()
table.reloadData()
totalPrice = 0
priceLabel.text = String(format: "%.2f", totalPrice)
UserDefaults.standard.set(items, forKey: "items")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:1)
您在commit
方法中尝试了两次。一个签名错误,一个签名正确。您对警报的使用是错误的。您确实需要清理代码并删除所有错误的方法。然后将所需的功能放入正确的方法中。
为什么你在这么多方法参数上拥有所有那些不必要的!
运算符?
答案 1 :(得分:0)
您应该在
中显示提醒func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete {
items.remove(at: indexPath.row)
table.reloadData()
UserDefaults.standard.set(items, forKey: "items")
}
}
完整代码:
import UIKit
class CheckoutViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var table: UITableView!
@IBOutlet weak var checkoutButton: UIButton!
@IBOutlet weak var priceLabel: UILabel!
var items: [String] = []
internal func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")
cell.textLabel?.numberOfLines = 16
cell.textLabel?.font = cell.textLabel?.font.withSize(10)
cell.textLabel?.text = items[indexPath.row]
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(_ animated: Bool) {
priceLabel.text = String(format: "%.2f", totalPrice)
let itemsObject = UserDefaults.standard.object(forKey: "items")
if let tempItems = itemsObject as? [String] {
items = tempItems
}
table.reloadData()
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete {
items.remove(at: indexPath.row)
table.reloadData()
UserDefaults.standard.set(items, forKey: "items")
let alertController = UIAlertController(title: "Sample Title", message: "Sample message", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "Ok", style: .default, handler: nil)
alertController.addAction(defaultAction)
present(alertController, animated: true, completion: nil)
}
}
@IBAction func checkoutClicked(_ sender: Any) {
priceLabel.text = String(format: "%.2f", totalPrice)
items.removeAll()
table.reloadData()
totalPrice = 0
priceLabel.text = String(format: "%.2f", totalPrice)
UserDefaults.standard.set(items, forKey: "items")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}