无法让Cell Swipe Actions工作

时间:2017-05-24 19:30:43

标签: swift3 uitableviewrowaction ios10.3

自从我在这里愚弄自己已经有一段时间了,所以这里又一次又一次,因为我还在努力学习,感觉好像我正在追逐这个项目的尾巴。

话虽如此,最终我想让Jerkoch的SwipeCellKit工作,但此时没有必要,因为我只需要弄清楚除了'删除'单元格动作之外如何添加更多选项。

这就是我所拥有的:My Project

这就是我想要的:Jerkoch's Project

如果我在

的tableView上设置delegate属性,我会丢失数据
let cell = tableView.dequeueReusableCell(withIdentifier: "CommentCell", for: indexPath) as! CommentTableViewCell

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

这是我的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]? {

我做错了什么,或者我需要CUD才能获得3次滑动操作;删除,标记和阻止,以使Apple对我的下一次提交感到满意。

提前谢谢大家!

1 个答案:

答案 0 :(得分:0)

这个问题已经过时,但我会写下我认为可以解决的答案:)

你应该使用UITableViewController而不是UIViewController作为你的视图。 SwipeCellKit仅适用于TableViewControllers。