swift - 在 Swift 中,如何在从 UITableView 中删除项目时触发事件发生?

标签 swift uitableview

我使用的是 Swift 3.1,我的一个 View Controller 中有一张表。当用户通过向左滑动并按下红色删除按钮删除表中的一项时,我希望弹出一条警告消息并说些什么。当我向左滑动以从我的 UITableView 表中删除某些内容时,由于某种原因没有触发我下面的代码。请参阅下面我用“CODE HERE”标记我的尝试的 10 行左右。有什么方法可以让我在删除某些内容时触发此代码?

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 方法有两次尝试。一份签名错误,一份签名正确。您对警报的使用是错误的。你真的需要清理你的代码并删除所有错误的方法。然后将所需的功能放入正确的方法中。

为什么在如此多的方法参数上使用所有那些不必要的 ! 运算符?

关于swift - 在 Swift 中,如何在从 UITableView 中删除项目时触发事件发生?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43702743/

相关文章:

ios - 双重感叹!!在 Swift 中标记?

ios - Firebase 连接管理器应该只返回一个结果

ios - 未调用 headerViewForSection

ios - 如何 UISearchcontroller customcell swift

ios - 我们可以在 Xcode 6 beta 中将 swift 与 objective - c 一起使用吗?

swift - 使用 JsonDecoder 和 Alamofire 解码 Json 数据

ios - 使用 Storyboard的 DidSelectRowAtIndexPath

ios - 为什么 UITableView commitEditing 不起作用?

ios - LayoutSubviews 问题

ios - Xcode 10.2,Swift 5,使用 Release Scheme 构建程序时命令 compileSwift 失败