ios - TableView 操作不显示

标签 ios swift uitableview

我用这些代码创建了一个 UITableViewRowAction:

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

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

        })

    if editingStyle == UITableViewCellEditingStyle.Delete {

        let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let managedContext = delegate.managedObjectContext!

        var error: NSError?

        let fetchRequest = NSFetchRequest(entityName: "Task")

        let fetchedResults = managedContext.executeFetchRequest(fetchRequest, error: &error) as! [NSManagedObject]

        managedContext.deleteObject(fetchedResults[indexPath.row])
        table.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)

    }

当我运行它时,可以向左滑动行,但没有显示任何按钮。

我添加了这个:

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {

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

    })

    deleteAction.backgroundColor = UIColor.redColor()

    return [deleteAction]
}

并且删除了commitEditingStyle中的代码,还是没有显示。

最佳答案

如果你想要UITableviewRowAction,你委托(delegate)的方法不对

试试这个

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

}
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
    let action = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "First") { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
        //Do action here
    }
    action.backgroundColor = UIColor .greenColor()
      return [action]
}

截图

enter image description here

关于ios - TableView 操作不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30836247/

相关文章:

ios - Collectionview 内部 tableview 单元格索引路径问题

ios - NSNotificationCenter 在 vi​​ewDidAppear 中的使用 :

ios - 从左到右标记在 Swift 中不起作用

ios - 想要通过从 firebase 下载数据来显示,在 tableView 中添加一个 collectionView 。 ios 应用程序 swift

ios - 带有静态单元格的 UITableView 中的自定义单元格类

ios - Swift 4 从 HealthKit 获取 preferredUnit

ios - UITableView 自定义单元格重叠

ios - UITableViewCell 没有调整到 UIImageView

ios - LLVM、GCC 4.2 和 Apple LLVM 编译器 3.1 之间的区别

ios - 静态 UITableViewCell 中的动态 UITableView