我有 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/