Search code examples
iosswiftuitableviewrowaction

Cannot find a initializer for type "UITableViewRowAction"


How come it keeping showing me this message even though I type the right declaration of UITableViewRowAction?

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

Solution

  • The Xcode 6.4 documentation says both parameters should be implicitly unwrapped optionals.

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

    or in Xcode 7, neither parameters are optional.

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

    Either way, you should try

    var shareAction = UITableViewRowAction(style: .Default, title: "Share") {
        action, indexPath in
        /* ... */
    }
    

    Which is a more swifty way of doing it.