删除动画不显示

deleting animation doesn't show up

我有 UIViewController,我在里面拖了一个 UITableView,我想向 table 插入一些元素,并能够在此处删除它们。当我使用这段代码时,它适用于 UITableViewController 但现在当我想删除一个单元格时,我可以交换它但是删除动画(红色标志)没有显示

import UIKit

class ViewController: UIViewController , UITableViewDataSource , UITableViewDelegate {

    var listArray = ["Danial"]
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBOutlet var table: UITableView!

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listArray.count
    }


    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = UITableViewCell()

        cell.textLabel?.text = listArray[indexPath.row]
        return cell
    }

    func tableView(tableView: UITableView!, canEditRowAtIndexPath indexPath: NSIndexPath!) -> Bool
    {
        return true
    }

    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if (editingStyle == UITableViewCellEditingStyle.Delete){
            listArray.removeAtIndex(indexPath.row)
     tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)        }
    }

}

您的代码存在问题,您输入的是:

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if (editingStyle == UITableViewCellEditingStyle.Delete){
        listArray.removeAtIndex(indexPath.row)
 tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)        }
}

而不是

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if (editingStyle == UITableViewCellEditingStyle.Delete){
        listArray.removeAtIndex(indexPath.row)
 self.table.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)        }
}

所以只要去你写过的地方

tableView.

并将其更改为 self.table. (basically what you named your tableview from the IBOutlet)