UICollectionView在collectionView之间拖放单元格

时间:2018-01-02 21:39:25

标签: ios swift uicollectionview

您好我在两个三个不同的collectionView之间使用KDDragAndDropCollectionView进行拖放功能。一切都很好,但我不能限制特定情况下的运动。我有三个collectionView。用户可以从A下降到B,从B下降到C.但是他不能从A到C或C从B到B或B到A。

Here is my code.

import SlideMenuControllerSwift

class MainViewController: NavigationBarViewController,KDDragAndDropCollectionViewDataSource {
    @IBOutlet weak var inProgressView: UIView!

    @IBOutlet weak var doneview: UIView!
    @IBOutlet weak var toDoView: UIView!
    @IBOutlet weak var doneCollectionView: UICollectionView!

    @IBOutlet weak var inProgressCollectionView: UICollectionView!

    @IBOutlet weak var toDoCollectionView: UICollectionView!
    var drop: UIDropDown!
    var toDoDataArray = [String]()
    var inProgressDataArray = [String]()
    var doneDataArray = [String]()
    var dragAndDropManager : KDDragAndDropManager?


    //MARK: - View Life Cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        toDoDataArray.append("A")
        toDoDataArray.append("B")
        toDoDataArray.append("C")

        inProgressDataArray.append("D")

        doneDataArray.append("B")
        doneDataArray.append("C")




        // Do any additional setup after loading the view.
        self.setUp()
        self.setupDropDown()
         self.dragAndDropManager = KDDragAndDropManager(canvas: self.view, collectionViews: [toDoCollectionView, inProgressCollectionView,doneCollectionView])
    }


    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
    }


    //MARK: - Private Method

    func setupDropDown() {
        drop = UIDropDown(frame: CGRect(x: 24, y: 80, width: 200, height: 40))
        //drop.center = CGPoint(x: self.view.frame.midX, y: self.view.frame.midY)
        drop.placeholder = "Select Month"
        drop.options = ["Weekly", "Monthly", "Bi-Annual", "Annual"]
        drop.didSelect { (option, index) in
            self.drop.placeholder = option
            print("You just select: \(option) at index: \(index)")
        }
        self.view.addSubview(drop)
    }


    func setUp()
    {
        //Setup Navigation Bar
        self.menuIconImage = #imageLiteral(resourceName: "hamIco")
        self.setNavigationBarButtonItem()
        self.setNavigationBarItem()
    }

    /*
     // MARK: - Navigation

     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */

    //MARK: - Navigation Bar Button Action Method

    //MARK: - Button Action Methods
    /**
     Button Action method. Gets called when the left navigation item
     Parameters: sender - the button on which the event occurred
     **/
    @IBAction func leftBtnAction(sender: UIButton) {
        self.toggleLeft()
    }


    // MARK: - UITableView DataSource abd Delegate

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        print("Step 1 called")
        if collectionView.tag == 0 {
            return toDoDataArray.count
        }
        else  if collectionView.tag == 1 {
            return inProgressDataArray.count
        }
        return doneDataArray.count
    }

    // The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        print("Step 2 called")

        if collectionView.tag == 0 {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TO_DO_COLLECTION_CELL_ID, for: indexPath) as! ToDoCollectionViewCell
            cell.isHidden = false

            if let kdCollectionView = collectionView as? KDDragAndDropCollectionView {

                if let draggingPathOfCellBeingDragged = kdCollectionView.draggingPathOfCellBeingDragged {

                    if draggingPathOfCellBeingDragged.item == indexPath.item {

                        cell.isHidden = true

                    }
                }
            }
            return cell
        }
        else  if collectionView.tag == 1 {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: IN_PROGRESS_COLLECTION_CELL_ID, for: indexPath) as! InProgressCollectionViewCell
            cell.isHidden = false

            if let kdCollectionView = collectionView as? KDDragAndDropCollectionView {

                if let draggingPathOfCellBeingDragged = kdCollectionView.draggingPathOfCellBeingDragged {

                    if draggingPathOfCellBeingDragged.item == indexPath.item {

                        cell.isHidden = true

                    }
                }
            }
            return cell
        }
        else{
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: DONE_COLLECTION_Cell_ID, for: indexPath) as! DoneCollectionViewCell
            cell.isHidden = false

            if let kdCollectionView = collectionView as? KDDragAndDropCollectionView {

                if let draggingPathOfCellBeingDragged = kdCollectionView.draggingPathOfCellBeingDragged {

                    if draggingPathOfCellBeingDragged.item == indexPath.item {

                        cell.isHidden = true

                    }
                }
            }
            return cell
        }



    }

    // MARK : KDDragAndDropCollectionViewDataSource

    func collectionView(_ collectionView: UICollectionView, dataItemForIndexPath indexPath: IndexPath) -> AnyObject {
        print("Step 3 called")
        if collectionView.tag == 0 {
            return toDoDataArray[indexPath.item] as AnyObject
        }
        else  if collectionView.tag == 1 {
            return inProgressDataArray[indexPath.item] as AnyObject
        }
        return doneDataArray[indexPath.item] as AnyObject
        //return data[collectionView.tag][indexPath.item]
    }
    func collectionView(_ collectionView: UICollectionView, insertDataItem dataItem : AnyObject, atIndexPath indexPath: IndexPath) -> Void {
        print("Step 4 called")
        if collectionView.tag == 0 {
            if let di = dataItem as? String {
                toDoDataArray.insert(di, at: indexPath.item)
                //data[collectionView.tag].insert(di, at: indexPath.item)
            }
        }
        else  if collectionView.tag == 1 {
            if let di = dataItem as? String {
                inProgressDataArray.insert(di, at: indexPath.item)
                //data[collectionView.tag].insert(di, at: indexPath.item)
            }
        }
        else{
            if let di = dataItem as? String {
                doneDataArray.insert(di, at: indexPath.item)
            }
        }





    }
    func collectionView(_ collectionView: UICollectionView, deleteDataItemAtIndexPath indexPath : IndexPath) -> Void {
        print("Step 5 called")
        if collectionView.tag == 0 {
            toDoDataArray.remove(at: indexPath.item)
        }
        else  if collectionView.tag == 1 {
            inProgressDataArray.remove(at: indexPath.item)
        }
        else{
          doneDataArray.remove(at: indexPath.item)
        }

    }

    func collectionView(_ collectionView: UICollectionView, moveDataItemFromIndexPath from: IndexPath, toIndexPath to : IndexPath) -> Void {
        print("Step 6 called")
        if collectionView.tag == 0 {
            let fromDataItem: String = toDoDataArray[from.item]
            toDoDataArray.remove(at: from.item)
            toDoDataArray.insert(fromDataItem, at: to.item)
        }
        else  if collectionView.tag == 1 {
            let fromDataItem: String = inProgressDataArray[from.item]
            inProgressDataArray.remove(at: from.item)
            inProgressDataArray.insert(fromDataItem, at: to.item)
        }
        else{
            let fromDataItem: String = doneDataArray[from.item]
            doneDataArray.remove(at: from.item)
            doneDataArray.insert(fromDataItem, at: to.item)
        }


    }

    func collectionView(_ collectionView: UICollectionView, indexPathForDataItem dataItem: AnyObject) -> IndexPath? {
        print("Step 7 called")

        if collectionView.tag == 0 {
            if let candidate : String = dataItem as? String {

                for item : String in toDoDataArray {
                    if candidate  == item {

                        let position = toDoDataArray.index(of: item)! // ! if we are inside the condition we are guaranteed a position
                        let indexPath = IndexPath(item: position, section: 0)
                        return indexPath
                    }
                }
            }
        }
        else  if collectionView.tag == 1 {
            if let candidate : String = dataItem as? String {

                for item : String in inProgressDataArray {
                    if candidate  == item {

                        let position = inProgressDataArray.index(of: item)! // ! if we are inside the condition we are guaranteed a position
                        let indexPath = IndexPath(item: position, section: 0)
                        return indexPath
                    }
                }
            }
        }
        else{
            if let candidate : String = dataItem as? String {

                for item : String in doneDataArray {
                    if candidate  == item {

                        let position = doneDataArray.index(of: item)! // ! if we are inside the condition we are guaranteed a position
                        let indexPath = IndexPath(item: position, section: 0)
                        return indexPath
                    }
                }
            }
        }



        return nil

    }


}


//MARK: - Extension written for Slide the Menu
extension MainViewController : SlideMenuControllerDelegate {

    func leftWillOpen() {
        print("SlideMenuControllerDelegate: leftWillOpen")
    }

    func leftDidOpen() {
        print("SlideMenuControllerDelegate: leftDidOpen")
    }

    func leftWillClose() {
        print("SlideMenuControllerDelegate: leftWillClose")
    }

    func leftDidClose() {
        print("SlideMenuControllerDelegate: leftDidClose")
    }

    func rightWillOpen() {
        print("SlideMenuControllerDelegate: rightWillOpen")
    }

    func rightDidOpen() {
        print("SlideMenuControllerDelegate: rightDidOpen")
    }

    func rightWillClose() {
        print("SlideMenuControllerDelegate: rightWillClose")
    }

    func rightDidClose() {
        print("SlideMenuControllerDelegate: rightDidClose")
    }




}

0 个答案:

没有答案