Swift uicollectionview通过拖动一次选择多个连续项目

时间:2018-08-21 08:44:00

标签: ios swift

我做一个日记日历。 我想一拖多选。 好像在某个电话廊中删除多个项目时,我拖动一些连续的项目以删除多个项目。

apple uicollectionview allowMultipleSelection无法正常运行...

如果不立即支持?

我使用JTAppleCalendar 7.1.5,swift 4,xcode 9。

这是我的视图控制器代码

import UIKit
import JTAppleCalendar
import SideMenu

class ViewController: UIViewController {
    let formatter = DateFormatter()
    @IBOutlet var collectionView: JTAppleCalendarView!

    @IBOutlet var multiSelectButton: UISwitch!

    @IBOutlet var navigationItemBar: UINavigationItem!
    override func viewDidLoad(){
        super.viewDidLoad()
        self.edgesForExtendedLayout = []

        collectionView.scrollToDate(Date(), animateScroll: false)

        setupCalendarView()
        SideMenuManager.default.menuWidth = round((UIScreen.main.bounds.width) * 0.25)

//        if multiSelectButton.isOn {
//            collectionView.allowsMultipleSelection = true
//        } else {
//            collectionView.allowsMultipleSelection = false
//        }
    }

    func setupCalendarView(){
        //Setup calendar spacing
        collectionView.minimumLineSpacing = 0
        collectionView.minimumInteritemSpacing = 0

        //Setup labels
        collectionView.visibleDates { (visibleDates) in
            self.setupViewsOnCalendar(from: visibleDates)
        }

        collectionView.scrollDirection = .vertical
    }

    func setupViewsOnCalendar(from visibleDates: DateSegmentInfo){
        let date = visibleDates.monthDates.first!.date

        self.formatter.dateFormat = "MM"
        self.navigationItemBar.title = self.formatter.string(from:date)

    }

    func handleCellTextColor(view: JTAppleCell?, cellState: CellState){
        guard let validCell = view as? CustomCell else { return }
        if cellState.dateBelongsTo == .thisMonth {
            validCell.dateLabel.textColor = UIColor.black
        } else {
            validCell.dateLabel.textColor = UIColor.gray
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

extension ViewController: JTAppleCalendarViewDataSource {

    func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters {
        formatter.dateFormat="yyyy MM dd"
        formatter.timeZone=Calendar.current.timeZone
        formatter.locale = Calendar.current.locale

        let startDate = formatter.date(from: "2017 01 01")!
        let endDate = formatter.date(from: "2019 12 31")!

        let parameters = ConfigurationParameters(startDate: startDate, endDate: endDate)
        return parameters
    }
}

extension ViewController: JTAppleCalendarViewDelegate {
    //display the cell
    func calendar(_ calendar: JTAppleCalendarView, willDisplay cell: JTAppleCell, forItemAt date: Date, cellState: CellState, indexPath: IndexPath) {
        let mycell = cell as! CustomCell
        mycell.dateLabel.text = cellState.text
        handleCellTextColor(view: cell, cellState: cellState)

        formatter.dateFormat = "yyy MM dd" // or whatever format you want.

        let currentDateString = formatter.string(from: Date())
        let cellStateDateString = formatter.string(from: cellState.date)

        if  currentDateString ==  cellStateDateString {
            // this
            mycell.backgroundColor = UIColor.red
        } else {
            // that
            mycell.backgroundColor = UIColor.white
        }


    }

    func calendar(_ calendar: JTAppleCalendarView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell {
        let mycell = calendar.dequeueReusableJTAppleCell(withReuseIdentifier: "CustomCell", for: indexPath) as! CustomCell
        mycell.dateLabel.text = cellState.text
        mycell.layer.borderColor = UIColor.black.cgColor
        mycell.layer.borderWidth = 1

        self.calendar(calendar, willDisplay: mycell, forItemAt: date, cellState: cellState, indexPath: indexPath)
        return mycell
    }

    func calendar(_ calendar: JTAppleCalendarView, didSelectDate date: Date, cell: JTAppleCell?, cellState: CellState) {
//        self.performSegue(withIdentifier: "MonthToWeekSegue", sender: self)
        cell?.backgroundColor = UIColor.blue
    }

    func calendar(_ calendar: JTAppleCalendarView, didScrollToDateSegmentWith visibleDates: DateSegmentInfo) {
        setupCalendarView()
    }
}

0 个答案:

没有答案