如何使用swift 2.2从tableview中删除一行

时间:2016-12-09 09:34:35

标签: ios swift tableview delete-row

我在我的应用程序中使用swift开发应用程序我正在使用tableview添加联系人现在我想从表视图中删除一行我已经检查了其他堆栈溢出答案但是没有任何问题可供我使用

我的表视图控制器中的

代码:

 func
tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool

  {

        return true
    }

  functableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)


  {

        if (editingStyle == UITableViewCellEditingStyle.Delete)
        {
            dataArray.removeAtIndex(indexPath.row)
            self.FirstTableView.reloadData()
        }
    }

这个对我和单元格没有帮助甚至没有移动并显示删除按钮

注意:1.am使用左侧导航抽屉,所以我无法向左侧滑动

2.我只想在右侧滑动

3.am没有添加任何约束使用大小类和自动布局

1 个答案:

答案 0 :(得分:1)

你检查你定义的这个委托方法(我想你错过了这个)

   func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {

    return UITableViewCellEditingStyle.Delete
}

并在单元格提交样式中删除这样的数据:

        let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
        let context:NSManagedObjectContext = appDel.managedObjectContext!
        context.deleteObject(dataarray[indexPath.row] as NSManagedObject)
        dataarray.removeAtIndex(indexPath.row)
        context.save(nil)
         self.FirstTableView.reloadData()