我用这些代码创建了一个 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]
}
截图
关于ios - TableView 操作不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30836247/