2016-01-23 65 views
2

我有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)  } 
    } 

} 

回答

0

问题你打字:中

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)

+0

哦...我的坏,但即使我纠正一个仍然无法获得红色标志。你知道为什么吗? :) –

+0

做了那个工作还是仍然有麻烦? –

+0

我发现我的第二个错误,那就是我没有定义任何约束,所以它刚刚离开屏幕XD,但非常感谢您的帮助;) –