我使用FSCalendar来处理日期,并根据所选日期显示时间。
我在这个方法中显示了我的日期
func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition)
当我选择日期时,我会在此方法中显示选择时间的时间
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
因此,当我选择一个周末的日期时,我需要显示与工作日不同的其他时间。例如:
28是周末 - 周日我想要显示这个时间但如果我选择29是工作日 - 周一我想显示这个时间
等每个周末或工作日
现在我只能为所有日期显示同一时间。
我的代码:
class BookingViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, FSCalendarDelegate, FSCalendarDataSource, FSCalendarDelegateAppearance {
@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var calendar: FSCalendar!
@IBOutlet weak var selectedTimeLabel: UILabel!
@IBOutlet weak var selectedDateLabel: UILabel!
var bookingHall: Halls?
var timeIntervalArray: [String] = []
var selectedTimeIntervalArray: [String] = []
let curDate = Date().addingTimeInterval(-24*60*60)
var allArrayTimesAndPrices: [(time: String, price: Int)] = []
lazy var dateFormatter: DateFormatter = {
var formatter = DateFormatter()
formatter.dateFormat = "dd/MM/yyyy"
return formatter
}()
lazy var timeFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "HH:mm"
return formatter
}()
override func viewDidLoad() {
super.viewDidLoad()
if let timeRange = generateTimeRange() {
self.timeIntervalArray = timeRange
self.collectionView.reloadData()
} else {
// Handle error
}
if let hall = bookingHall {
allArrayTimesAndPrices = timeIntervalArray.map({ (time: $0, price: hall.price) })
}
}
// MARK: - CollectionView
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return allArrayTimesAndPrices.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "timeCell", for: indexPath) as! BookingTimeCell
cell.timeLabel.text = allArrayTimesAndPrices[indexPath.item].time
cell.priceLabel.text = "\(allArrayTimesAndPrices[indexPath.item].price) руб."
return cell
}
func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
let selectedDate = dateFormatter.string(from: date)
selectedDateLabel.text = "Дата: \(selectedDate)"
configCollectionViewAndLabelIsHidden(collectionView: false, textLabel: true, subTextLabel: true)
calendar.setCurrentPage(date, animated: true)
if date.isWeekend {
print("weekend")
} else {
print("weekdays")
}
self.collectionView.reloadData()
}
func generateTimeRange() -> [String]? {
var result: [String] = []
if let hall = bookingHall {
guard var startTime = timeFormatter.date(from: hall.firstStartTimeToIncreasePrice) else { return nil }
guard let endTime = timeFormatter.date(from: hall.firstEndTimeToIncreasePrice) else { return nil }
while startTime <= endTime {
result.append(timeFormatter.string(from: startTime))
startTime = Calendar.current.date(byAdding: .hour, value: 1, to: startTime)!
}
}
return result
}
}
现在我可以使用此代码在此方法func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition)
上在控制台的不同日期(周末或工作日)上显示:
if date.isWeekend {
print("weekend")
} else {
print("weekdays")
}
但是我不明白我怎样才能在周末或工作日显示不同的时间?