轻扫tableViewCell以删除项目

时间:2016-07-21 12:45:38

标签: swift xcode

我想从核心数据应用程序中删除一个单元。当用户按下Cell上的删除按钮时,我会发出警告。

代码是

    let alert = UIAlertController(title: "Warning!", message: "Are you sure to delete \"\(currentUnit)\" and all of its containing objects?", preferredStyle: .Alert)

我想用对象计数更新警告消息,所以我写了

 currentUnitObjectsCount = getSelectedUnitObjectsCount() //Which connects to database and returns just an integer

let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete",handler: { (action, indexPath) -> Void in

        let currentUnit = self.units[indexPath.row].title!

        let alert = UIAlertController(title: "Warning!", message: "Are you sure to delete \"\(currentUnit)\" and all of its \(currentUnitObjectsCount) containing objects?", preferredStyle: .Alert)

        let yesAction = UIAlertAction(title: "Yes", style: .Default) /* handler: { (action : UIAlertAction) -> Void in */ { (action : UIAlertAction) in                
            if let managedObjectContext = (UIApplication.sharedApplication().delegate as? AppDelegate)?.managedObjectContext{

                let unitToDelete = self.fetchResultController.objectAtIndexPath(indexPath) as! Unit

                managedObjectContext.deleteObject(unitToDelete)



                do{
                    try managedObjectContext.save()
                }catch{
                    print(error)
                }
            }

        self.checkUnits()

        }

        let noAction = UIAlertAction(title: "No", style: UIAlertActionStyle.Default, handler: {
            (action : UIAlertAction!) -> Void in

            tableView.reloadData() //to return the tableView to its normal view

        })

        alert.addAction(yesAction)

        alert.addAction(noAction)

        self.presentViewController(alert, animated: true, completion: nil)

               })
        deleteAction.backgroundColor = UIColor.redColor()

        return [editAction]
    }

现在,当我滑动tableViewCell时,它会滑动但是删除按钮不会出现,但是当我点击滑动单元格中的删除按钮位置时,会出现警告,这意味着按钮在那里,但我看不到它。

将代码返回到第一个样式,每个东西都可以正常工作,但第二个代码很奇怪。

当我把这行代码放在

currentUnitObjectsCount = getSelectedUnitObjectsCount() //Which connects to database and returns just an integer

之后的

    let deleteAction = ...

currentUnitObjectsCount = getSelectedUnitObjectsCount()
 let currentUnit = self.units[indexPath.row].title!
.
.
.

并滑动一个单元格,表格的行消失

我不知道为什么会发生这种情况。

0 个答案:

没有答案