在Swift中,从UITableView中删除项目时,如何触发事件发生?
我使用的是Swift 3.1,其中一个ViewController中有一个表。当用户通过向左滑动并按下红色的删除按钮删除表中的一个项目时,我希望弹出一条警告消息并说些什么。当我向左滑动以从UITableView表中删除某些内容时,由于某种原因,下面的代码没有被触发。请参阅下面的10行左右,我在其中用“此处代码”标记了我的尝试。有没有什么方法可以在删除某些内容时触发此代码在Swift中,从UITableView中删除项目时,如何触发事件发生?,swift,uitableview,Swift,Uitableview,我使用的是Swift 3.1,其中一个ViewController中有一个表。当用户通过向左滑动并按下红色的删除按钮删除表中的一个项目时,我希望弹出一条警告消息并说些什么。当我向左滑动以从UITableView表中删除某些内容时,由于某种原因,下面的代码没有被触发。请参阅下面的10行左右,我在其中用“此处代码”标记了我的尝试。有没有什么方法可以在删除某些内容时触发此代码 import UIKit class CheckoutViewController: UIViewController, U
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.
}
}
您有两次尝试
commit
方法。一个签名错误,一个签名正确。您使用的警报是错误的。您确实需要清理代码并删除所有错误的方法。然后将所需的功能放入正确的方法中
为什么你有那么多不必要的
在这么多的方法参数上编码>运算符?您应该在
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.
}
}
UserDefaults
的用途是什么?为什么只为了删除一行而重新加载整个表?@rmaddy这是提供的代码中的方法,不是我的解决方案。我没有足够的向下滚动。代码中实际上有两个committeeditingstyle
方法-一个签名错误,另一个签名正确。@rmaddy是的,有两个(:@shampouya我很高兴我帮了忙。请检查第二个答案,rmaddy给出了一些好的建议