Swift3 Can';无法使单元格滑动操作正常工作

Swift3 Can';无法使单元格滑动操作正常工作,swift3,uitableviewrowaction,ios10.3,Swift3,Uitableviewrowaction,Ios10.3,我已经有一段时间没有在这里出丑了,所以我还是在努力学习,感觉好像在这个项目上追逐我的尾巴 话虽如此,我最终还是想让Jerkoch的SwipeCellKit发挥作用,但目前还没有必要,因为我只需要找出如何添加更多选项,而不仅仅是“删除”单元格操作 这就是我得到的: 这就是我想要的: 如果从中设置tableView的委托属性,则会丢失数据 let cell = tableView.dequeueReusableCell(withIdentifier: "CommentCell", for: inde

我已经有一段时间没有在这里出丑了,所以我还是在努力学习,感觉好像在这个项目上追逐我的尾巴

话虽如此,我最终还是想让Jerkoch的SwipeCellKit发挥作用,但目前还没有必要,因为我只需要找出如何添加更多选项,而不仅仅是“删除”单元格操作

这就是我得到的:

这就是我想要的:

如果从中设置tableView的委托属性,则会丢失数据

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

以下是我的CommentViewController代码:

    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]
    }        
}
我甚至尝试过以下方法:

    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]   
}
无论我做什么,我都只能在刷卡操作中显示一般的“删除”操作

我甚至不认为正在调用/引用以下函数:

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
我做错了什么,或者我需要反刍什么才能得到3次刷卡动作;删除、标记和阻止,让苹果为我的下一次提交感到高兴


提前谢谢大家

这个问题已经过时了,但我会写一个答案,我认为这会起作用:)


您的视图应该使用UITableViewController而不是UIViewController。SwipeCellKit仅适用于TableViewController。

我很想帮助您,但现在在这个问题中似乎有很多额外的代码。请看一看StackOverflow帖子,了解如何创建一个最小的、完整的和可验证的示例:另外,添加一个问题,因为我不确定你在问什么。我根本无法让任何额外的刷卡操作起作用。使用或不使用带有我的代码的插件(swipecellkit)。我将看一看帖子示例:我只是想,如果我提交了整个VC,那么就更容易看到整个流程。谢谢
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {