如何在表格行上实现前导滑动手势?

时间:2019-05-06 00:56:03

标签: ios swift uitableview swipe

我实现了commmit editStyle,它允许我从表和Firebase数据库中删除记录。我现在正在尝试实现向右滑动的功能,该功能表示完成,然后在用户单击它之后实现代码。当我尝试“向右”滑动以获取领先动作时,什么也没显示。

class HoneyDoList: UITableViewController, FUIAuthDelegate, GADBannerViewDelegate  {

@IBOutlet weak var doListTableView: UITableView!

var repairList = [RepairModel]()
var refRepairs: DatabaseReference?
var UserID = Auth.auth().currentUser?.uid
var bannerView: GADBannerView!


override func viewDidLoad() {
    super.viewDidLoad()

    //implement Google ADMobs
            // In this case, we instantiate the banner with desired ad size.
            bannerView = GADBannerView(adSize: kGADAdSizeBanner)

            addBannerViewToView(bannerView)
            bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
            bannerView.rootViewController = self
            bannerView.load(GADRequest())
            bannerView.delegate = self

    //get reference to firebase database
    refRepairs = Database.database().reference().child("repairs").child(UserID!);


        //observing the data changes
        refRepairs!.observe(DataEventType.value, with: { (snapshot) in
        //if the reference have some values
        if snapshot.childrenCount > 0 {

            //clearing the list
            self.repairList.removeAll()

            //iterating through all the values
            for repairs in snapshot.children.allObjects as! [DataSnapshot] {
                //getting values
                let repairObject = repairs.value as? [String: AnyObject]
                let brand  = repairObject?["brand"]
                let uid = repairObject?["uid"]
                let id  = repairObject?["id"]
                let category = repairObject?["category"]
                let modelNumber = repairObject?["modelNumber"]

                //creating artist object with model and fetched values
                let repair = RepairModel(uid: uid as! String?, id: id as! String?, category: category as! String?, brand: brand as! String?, modelNumber: modelNumber as! String?)

                //appending it to list
                print(snapshot.childrenCount)

                self.repairList.append(repair)
            }

            //reloading the tableview
            self.doListTableView.reloadData()
        }
    })
}

func tableview(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let Complete = UIContextualAction(style: .normal, title: "Complete") { (action, view, nil) in
        print("completed")
    }
    Complete.backgroundColor = #colorLiteral(red: 0.4666666687, green: 0.7647058964, blue: 0.2666666806, alpha: 1)
    return UISwipeActionsConfiguration(actions: [Complete])
}

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! RepairTableViewCell
    let repair: RepairModel

    repair = repairList[indexPath.row]

    cell.labelBrand.text = repair.brand
    cell.labelModelNumber.text = repair.modelNumber

    return(cell)
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "showdetail", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let destination = segue.destination as? DetailVC {
        destination.repair = repairList[(doListTableView.indexPathForSelectedRow?.row)!]

    }
}

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {

    let repairList = self.repairList[indexPath.row]
    if let selectedRepairItem = repairList.id {
        Database.database().reference().child("repairs").child(UserID!).child(selectedRepairItem).removeValue()
    }
}



func addBannerViewToView(_ bannerView: GADBannerView) {
    bannerView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(bannerView)
    view.addConstraints(
        [NSLayoutConstraint(item: bannerView,
                            attribute: .bottom,
                            relatedBy: .equal,
                            toItem: bottomLayoutGuide,
                            attribute: .top,
                            multiplier: 1,
                            constant: 0),
         NSLayoutConstraint(item: bannerView,
                            attribute: .centerX,
                            relatedBy: .equal,
                            toItem: view,
                            attribute: .centerX,
                            multiplier: 1,
                            constant: 0)
        ])
}
/// Tells the delegate an ad request loaded an ad.
func adViewDidReceiveAd(_ bannerView: GADBannerView) {
    bannerView.alpha = 0
    UIView.animate(withDuration: 1, animations: {
        bannerView.alpha = 1
    })
}

/// Tells the delegate an ad request failed.
func adView(_ bannerView: GADBannerView,
            didFailToReceiveAdWithError error: GADRequestError) {
    print("adView:didFailToReceiveAdWithError: \(error.localizedDescription)")
}

/// Tells the delegate that a full-screen view will be presented in response
/// to the user clicking on an ad.
func adViewWillPresentScreen(_ bannerView: GADBannerView) {
    print("adViewWillPresentScreen")
}

/// Tells the delegate that the full-screen view will be dismissed.
func adViewWillDismissScreen(_ bannerView: GADBannerView) {
    print("adViewWillDismissScreen")
}

/// Tells the delegate that the full-screen view has been dismissed.
func adViewDidDismissScreen(_ bannerView: GADBannerView) {
    print("adViewDidDismissScreen")
}

/// Tells the delegate that a user click will open another app (such as
/// the App Store), backgrounding the current app.
func adViewWillLeaveApplication(_ bannerView: GADBannerView) {
    print("adViewWillLeaveApplication")
}


override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

}

我实现了“ leadingSwipeActionsConfigurationForRowAt”,但似乎什么也没发生。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

    let complete = UIContextualAction.init(style: .normal, title: "Complete") { (action, view, completion) in

        //...

        completion(true) // Completion
    }

    complete.image = //... Add an image
    complete.backgroundColor = //... Some color
    let action = UISwipeActionsConfiguration.init(actions: [complete])
    action.performsFirstActionWithFullSwipe = true //... Full swipe support

    return action
}