自定义日历视图 [英] Custom calender View
本文介绍了自定义日历视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想为我的应用程序开发自定义calendarView.我想我可以通过使用UICollectionView
来做到这一点:
I want to develop custom calendarView for my app. i think i can make it by use of UICollectionView
like this:
我尝试了这个简单的事情:
i tried this simple thing:
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 5 //for number of weeks
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 7 //for number of days in week
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AvailabilityCollectionViewCell", for: indexPath) as! AvailabilityCollectionViewCell
cell.lblDate.text = "\(indexPath.row)"
cell.layer.borderColor = UIColor.black.cgColor
cell.layer.borderWidth = 0.5
return cell
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
minimumInteritemSpacingForSectionAt section: Int) -> CGFloat{
return 0.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.size.width / 7.0, height: collectionView.frame.size.height / 5)
}
通过此操作,我得到如下简单的收藏夹视图":
by this i get simple Collection View like this:
现在,我想知道如何根据日历安排工作日以及为此需要什么样的数据源...
Now i want to know how can i arrange week days as per Calendar and what kind of datasource required for this...
谢谢..
推荐答案
变量:
var weeks = 0
var items = [[Date]]()
lazy var dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
return formatter
}()
设置日历的功能:
func setCalendar() {
let cal = Calendar.current
let components = (cal as NSCalendar).components([.month, .day,.weekday,.year], from: date)
let year = components.year
let month = components.month
let months = dateFormatter.monthSymbols
let monthSymbol = (months![month!-1])
lblMonth.text = "\(monthSymbol) \(year!)"
let weekRange = (cal as NSCalendar).range(of: .weekOfMonth, in: .month, for: date)
let dateRange = (cal as NSCalendar).range(of: .day, in: .month, for: date)
weeks = weekRange.length
totalDaysInMonth = dateRange.length
let totalMonthList = weeks * 7
var dates = [Date]()
var firstDate = dateFormatter.date(from: "\(year!)-\(month!)-1")!
let componentsFromFirstDate = (cal as NSCalendar).components([.month, .day,.weekday,.year], from: firstDate)
firstDate = (cal as NSCalendar).date(byAdding: [.day], value: -(componentsFromFirstDate.weekday!-1), to: firstDate, options: [])!
for _ in 1 ... totalMonthList {
dates.append(firstDate)
firstDate = (cal as NSCalendar).date(byAdding: [.day], value: +1, to: firstDate, options: [])!
}
let maxCol = 7
let maxRow = weeks
items.removeAll(keepingCapacity: false)
var i = 0
// print("-----------\(monthSymbol) \(year!)------------")
for _ in 0..<maxRow {
var colItems = [Date]()
for _ in 0..<maxCol {
colItems.append(dates[i])
i += 1
}
// print(colItems)
items.append(colItems)
}
// print("---------------------------")
}
代表职能:
extension AvailabilityCalenderViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return weeks
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 7
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "calendarCell", for: indexPath) as! calendarCell
cell.layer.borderColor = UIColor.black.cgColor
cell.layer.borderWidth = 0.5
cell.configureCell(date: items[indexPath.section][indexPath.row])
return cell
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
minimumInteritemSpacingForSectionAt section: Int) -> CGFloat{
return 0.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.size.width / 7.0, height: collectionView.frame.size.height / 5)
}
}
自定义集合视图单元格:
Custom Collection View Cell :
class calendarCell: UICollectionViewCell {
var date: Date!
@IBOutlet weak var lblDate: UILabel!
func configureCell(date: Date){
self.strDate = date
let cal = Calendar.current
let components = (cal as NSCalendar).components([.day], from: date)
let day = components.day!
self.lblDate.text = "\(String(describing: day))"
}
}
结果:
这篇关于自定义日历视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文