ios - 删除动画不显示

标签 ios swift uiviewcontroller tableview delete-row

我有 UIViewController,我在里面拖了一个 UITableView,我想向表格中插入一些元素并能够在此处删除它们。当我使用这段代码时,它适用于 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。 (基本上是您从 IBOutlet 中命名您的 tableview 的名称)

关于ios - 删除动画不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34958075/

相关文章:

ios - 使用 XCTest 检查变量是否为数组

ios - 如何将 UIEdgeInsets 应用于 CGImage?

ios - 代码中的错误导致按钮并不总是执行操作

ios - 将 subview 添加到导航 Controller 时未调用 viewdidappear

objective-c - UITableView : popViewController and keep row index into parent controller?

xcode - 无法将 UITableViewDataSouce 和 UITableViewDelegate 设置为 UIViewController

android - 使用 davibennun/laravel-push-notification 包的 Laravel 推送通知

ios - CCTransitions 与 Spritebuilder - Cocos2d

xml - 如何在 Swift 中首先在 UITableViewController 中执行解析器委托(delegate)

ios - 以编程方式禁用键盘声音