HungLikeaFieldMouse
HungLikeaFieldMouse

Reputation: 1

Can't get Cell Swipe Actions to work

It's been awhile since I've made a fool of myself here, so here goes again as I'm still trying to learn and feel as though I'm chasing my tail on this project.

With that said, ultimately I would like to get Jerkoch's SwipeCellKit to work, but not necessary at this point as I just need to figure out how to add more options other than just the 'delete' cell action.

This is what I've got: My Project

This is what I'd like: Jerkoch's Project

I loose my data if I set the delegate property on tableView from

let cell = tableView.dequeueReusableCell(withIdentifier: "CommentCell", for: indexPath) as! CommentTableViewCell

to

let cell = tableView.dequeueReusableCell(withIdentifier: "CommentCell", for: indexPath) as! SwipeTableViewCell

Here is my CommentViewController Code:

    import UIKit
    import SwipeCellKit

    class CommentViewController: UIViewController {

    var postId: String!
    var comments = [Comment]()
    var users = [User]()

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.tableFooterView = UIView(frame: CGRect.zero)
        tableView.separatorInset = UIEdgeInsets.zero

        tableView.dataSource = self
        tableView.estimatedRowHeight = 100
        tableView.rowHeight = UITableViewAutomaticDimension

        loadComments()
    }

    func loadComments() {
        Api.Post_Comment.REF_POST_COMMENTS.child(self.postId).observe(.childAdded, with: {
            snapshot in
            Api.Comment.observeComments(withPostId: snapshot.key, completion: {
                comment in
                self.fetchUser(uid: comment.uid!, completed: {
                    self.comments.append(comment)
                    self.tableView.reloadData()
                })
            })
        })
    }

    func fetchUser(uid: String, completed:  @escaping () -> Void ) {
        Api.User.observeUser(withId: uid, completion: {
            user in
            self.users.append(user)
            completed()
        })
    }


}

extension CommentViewController: UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return comments.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CommentCell", for: indexPath) as! CommentTableViewCell// Can't update this to SwipeTableViewCell - Will loose data
        cell.selectedBackgroundView = createSelectedBackgroundView()
        let comment = comments[indexPath.row]
        let user = users[indexPath.row]
        cell.comment = comment
        cell.user = user
        cell.delegate = self
        return cell
    }
}

extension CommentViewController: SwipeTableViewCellDelegate{

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

    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {
        guard orientation == .right else { return nil }

        let deleteAction = SwipeAction(style: .destructive, title: "Delete") { action, indexPath in
            // handle action by updating model with deletion
        }
        // customize the action appearance
        deleteAction.image = UIImage(named: "delete")

        let flagAction = SwipeAction(style: .destructive, title: "Flag") { action, indexPath in
            // handle action
        }
        deleteAction.image = UIImage(named: "flag")

        let blockAction = SwipeAction(style: .destructive, title: "Block") { action, indexPath in
            // handle action
        }
        deleteAction.image = UIImage(named: "block")

        return [deleteAction, flagAction, blockAction]
    }        
}

I've even tried something like the following:

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    // THIS GIVES THE DEFAULT 'DELETE' ACTION
    print("Commit editingStyle")

}

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

    let blockAction = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: "Block", handler: { (action:UITableViewRowAction, IndexPath:IndexPath) in
        //self.isEditing = false
        print("Block action Pressed")
    })

    let flagAction = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: "Flag", handler: { (action:UITableViewRowAction, IndexPath:IndexPath) in
        print("Flag action Pressed")
    })

    let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.destructive, title: "Delete", handler: { (action:UITableViewRowAction, IndexPath:IndexPath) in
        print("Delete action Pressed")
    })

    return [blockAction, flagAction, deleteAction]   
}

No matter what I do, I can't get more than just the generic 'Delete' action to show on a swipe action.

I don't even think the following function is being called/referenced:

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

What am I doing wrong, or what do I need to CUD to get 3 swipe actions; Delete, Flag and Block, to make Apple happy for my next submission.

Thank you all in advance!

Upvotes: 0

Views: 473

Answers (1)

emin
emin

Reputation: 319

This question is outdated but i'll write the answer which i think would have done the trick :)

You should be using UITableViewController not UIViewController for your view. SwipeCellKit only works with TableViewControllers.

Upvotes: 0

Related Questions