我使用FSCalendar处理日期并根据所选日期显示时间。
我用这种方法显示了我的日期
func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition)
当我选择日期时,我会在此方法中显示选择时间的时间
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
因此,当我选择周末的日期时,我需要显示与工作日不同的其他时间。例如:
但是如果我选择 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")
}
但我不明白如何在周末或工作日显示不同的时间?