DanieleLanari
DanieleLanari

Reputation: 111

Swiping on UITableViewCell

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
}

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
}

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
    let more = UITableViewRowAction(style: .normal, title: "More") { action, index in
        print("more button tapped")
    }
    more.backgroundColor = UIColor.lightGray

    let favorite = UITableViewRowAction(style: .normal, title: "Favorite") { action, index in
        print("favorite button tapped")
    }
    favorite.backgroundColor = UIColor.orange

    let share = UITableViewRowAction(style: .normal, title: "Share") { action, index in
        print("share button tapped")
    }
    share.backgroundColor = UIColor.blue

    return [share, favorite, more]
}

I have insert this code to create some actions to do on a single Cell, but when I run the project and I try to swipe on a cell the only action possible is Delete.

How can I fix this problem?

Upvotes: 1

Views: 502

Answers (1)

Nirav D
Nirav D

Reputation: 72410

Signature of tableView(_:editActionsForRowAt:) is changed in Swift 3 to like below. So simply replace your editActionsForRowAt with below one.

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

    let more = UITableViewRowAction(style: .normal, title: "More") { action, index in
        print("more button tapped")
    }
    more.backgroundColor = UIColor.lightGray

    let favorite = UITableViewRowAction(style: .normal, title: "Favorite") { action, index in
        print("favorite button tapped")
    }
    favorite.backgroundColor = UIColor.orange

    let share = UITableViewRowAction(style: .normal, title: "Share") { action, index in
        print("share button tapped")
    }
    share.backgroundColor = UIColor.blue

    return [share, favorite, more]
}

Upvotes: 2

Related Questions