Ios JTAppleCalendar以编程方式显示,单元格不显示
我试图在不使用故事板的情况下,通过编程实现JTAppleCalendar,并为单元使用xib文件。我已经设置了委托和数据源,并实现了所需的函数,但是单元格没有显示。我可以看到集合视图已设置并在Viewcontroller中可见(链接2中图像中的蓝色方框),并且调用了configureCalendar()和cellForItemAt(),但仍然没有显示任何数据 我错过了什么Ios JTAppleCalendar以编程方式显示,单元格不显示,ios,swift,uicollectionview,jtapplecalendar,Ios,Swift,Uicollectionview,Jtapplecalendar,我试图在不使用故事板的情况下,通过编程实现JTAppleCalendar,并为单元使用xib文件。我已经设置了委托和数据源,并实现了所需的函数,但是单元格没有显示。我可以看到集合视图已设置并在Viewcontroller中可见(链接2中图像中的蓝色方框),并且调用了configureCalendar()和cellForItemAt(),但仍然没有显示任何数据 我错过了什么 var calendarView: JTAppleCalendarView! override func vi
var calendarView: JTAppleCalendarView!
override func viewDidLoad() {
super.viewDidLoad()
calendarView = JTAppleCalendarView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
calendarView.register(CalendarCellView.self, forCellWithReuseIdentifier: "cell")
calendarView.ibCalendarDelegate = self
calendarView.ibCalendarDataSource = self
self.view.addSubview(calendarView)
self.view.bringSubview(toFront: calendarView)
self.calendarView.backgroundColor = UIColor.blue
}
extension TestViewController: JTAppleCalendarViewDataSource {
func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy MM dd"
let startDate = formatter.date(from: "2016 02 01")! // You can use date generated from a formatter
let endDate = Date() // You can also use dates created from this function
let parameters = ConfigurationParameters(startDate: startDate,
endDate: endDate,
numberOfRows: 6, // Only 1, 2, 3, & 6 are allowed
calendar: Calendar.current,
generateInDates: .forAllMonths,
generateOutDates: .tillEndOfGrid,
firstDayOfWeek: .sunday)
return parameters
}
func calendar(_ calendar: JTAppleCalendarView, willDisplay cell: JTAppleCell, forItemAt date: Date, cellState: CellState, indexPath: IndexPath) {
}
}
extension TestViewController: JTAppleCalendarViewDelegate {
func calendar(_ calendar: JTAppleCalendarView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell {
let cell = calendar.dequeueReusableJTAppleCell(withReuseIdentifier: "cell", for: indexPath) as! CalendarCellView
cell.dayLabel?.text = cellState.text
print(cellState.text)
return cell
}
}
class CalendarCellView: JTAppleCell {
@IBOutlet var dayLabel: UILabel!
@IBOutlet var taskImage: UIImageView!
override func awakeFromNib() {
dayLabel.textColor = UIColor.red
self.backgroundColor = UIColor.blue
}
}
布局
jAppleCalendarView
的超类是UICollectionView
,它使用UICollectionViewFlowLayout
来布局项目。jAppleCalendarView
提供了CellSize
来配置项目大小,您还可以使用UICollectionViewFlowLayout
来配置最小间距
使用自定义单元格
我猜您是在使用xib来定制单元格,所以您必须使用register(\unib:UINib?,forCellWithReuseIdentifier identifier:String)
来注册单元格。最后,别忘了为dayLabel添加约束
例子
calendarView的单元格大小为40,宽度为280(7列),高度为240(6行),单元格和行的最小间距为0。下面是一种可能的实现JTAppleClendar的方法,所有这些都可以通过代码实现,可能会对您有用:
注意:这只是启动和运行的起点,进一步的定制取决于您
JTAppleCalendar版本:8.0.3
在视图中加载:
let cal = JTACMonthView(frame: CGRect.zero)
cal.backgroundColor = .white
cal.cellSize = 20
cal.calendarDelegate = self
cal.calendarDataSource = self
cal.register(DateCell.self, forCellWithReuseIdentifier: "calenderCellID")
view.addSubview(cal)
cal.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([cal.centerXAnchor.constraint(equalTo: view.centerXAnchor),
cal.topAnchor.constraint(equalTo: view.topAnchor),
cal.widthAnchor.constraint(equalToConstant: 200),
cal.heightAnchor.constraint(equalToConstant: 200)])
class DateCell: JTACDayCell {
var dateLabel : UILabel = {
let dateLabel = UILabel()
dateLabel.text = "sample"
return dateLabel
}()
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(dateLabel)
dateLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([dateLabel.centerXAnchor.constraint(equalTo: centerXAnchor),
dateLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
dateLabel.heightAnchor.constraint(equalToConstant: 15)])
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
func calendar(_ calendar: JTACMonthView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTACDayCell {
let cell = calendar.dequeueReusableCell(withReuseIdentifier: calenderCellID, for: indexPath) as! DateCell
cell.dateLabel.text = cellState.text
return cell
}
func configureCalendar(_ calendar: JTACMonthView) -> ConfigurationParameters {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy MM dd"
let startDate = formatter.date(from: "2016 02 01")!
let endDate = Date()
let parameters = ConfigurationParameters(startDate: startDate,
endDate: endDate,
numberOfRows: 6, // Only 1, 2, 3, & 6 are allowed
calendar: Calendar.current,
generateInDates: .forAllMonths,
generateOutDates: .tillEndOfGrid,
firstDayOfWeek: .sunday)
return parameters
}
func calendar(_ calendar: JTACMonthView, willDisplay cell: JTACDayCell, forItemAt date: Date, cellState: CellState, indexPath: IndexPath) {
let cell = cell as! DateCell
cell.dateLabel.text = cellState.text
}
自定义单元格:
let cal = JTACMonthView(frame: CGRect.zero)
cal.backgroundColor = .white
cal.cellSize = 20
cal.calendarDelegate = self
cal.calendarDataSource = self
cal.register(DateCell.self, forCellWithReuseIdentifier: "calenderCellID")
view.addSubview(cal)
cal.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([cal.centerXAnchor.constraint(equalTo: view.centerXAnchor),
cal.topAnchor.constraint(equalTo: view.topAnchor),
cal.widthAnchor.constraint(equalToConstant: 200),
cal.heightAnchor.constraint(equalToConstant: 200)])
class DateCell: JTACDayCell {
var dateLabel : UILabel = {
let dateLabel = UILabel()
dateLabel.text = "sample"
return dateLabel
}()
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(dateLabel)
dateLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([dateLabel.centerXAnchor.constraint(equalTo: centerXAnchor),
dateLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
dateLabel.heightAnchor.constraint(equalToConstant: 15)])
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
func calendar(_ calendar: JTACMonthView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTACDayCell {
let cell = calendar.dequeueReusableCell(withReuseIdentifier: calenderCellID, for: indexPath) as! DateCell
cell.dateLabel.text = cellState.text
return cell
}
func configureCalendar(_ calendar: JTACMonthView) -> ConfigurationParameters {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy MM dd"
let startDate = formatter.date(from: "2016 02 01")!
let endDate = Date()
let parameters = ConfigurationParameters(startDate: startDate,
endDate: endDate,
numberOfRows: 6, // Only 1, 2, 3, & 6 are allowed
calendar: Calendar.current,
generateInDates: .forAllMonths,
generateOutDates: .tillEndOfGrid,
firstDayOfWeek: .sunday)
return parameters
}
func calendar(_ calendar: JTACMonthView, willDisplay cell: JTACDayCell, forItemAt date: Date, cellState: CellState, indexPath: IndexPath) {
let cell = cell as! DateCell
cell.dateLabel.text = cellState.text
}
最后,数据源和委托方法:
let cal = JTACMonthView(frame: CGRect.zero)
cal.backgroundColor = .white
cal.cellSize = 20
cal.calendarDelegate = self
cal.calendarDataSource = self
cal.register(DateCell.self, forCellWithReuseIdentifier: "calenderCellID")
view.addSubview(cal)
cal.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([cal.centerXAnchor.constraint(equalTo: view.centerXAnchor),
cal.topAnchor.constraint(equalTo: view.topAnchor),
cal.widthAnchor.constraint(equalToConstant: 200),
cal.heightAnchor.constraint(equalToConstant: 200)])
class DateCell: JTACDayCell {
var dateLabel : UILabel = {
let dateLabel = UILabel()
dateLabel.text = "sample"
return dateLabel
}()
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(dateLabel)
dateLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([dateLabel.centerXAnchor.constraint(equalTo: centerXAnchor),
dateLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
dateLabel.heightAnchor.constraint(equalToConstant: 15)])
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
func calendar(_ calendar: JTACMonthView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTACDayCell {
let cell = calendar.dequeueReusableCell(withReuseIdentifier: calenderCellID, for: indexPath) as! DateCell
cell.dateLabel.text = cellState.text
return cell
}
func configureCalendar(_ calendar: JTACMonthView) -> ConfigurationParameters {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy MM dd"
let startDate = formatter.date(from: "2016 02 01")!
let endDate = Date()
let parameters = ConfigurationParameters(startDate: startDate,
endDate: endDate,
numberOfRows: 6, // Only 1, 2, 3, & 6 are allowed
calendar: Calendar.current,
generateInDates: .forAllMonths,
generateOutDates: .tillEndOfGrid,
firstDayOfWeek: .sunday)
return parameters
}
func calendar(_ calendar: JTACMonthView, willDisplay cell: JTACDayCell, forItemAt date: Date, cellState: CellState, indexPath: IndexPath) {
let cell = cell as! DateCell
cell.dateLabel.text = cellState.text
}
注:根据:
这两个函数应该包含相同的代码,因此是明智的
具有共享功能以减少代码重复。唯一的
这两个函数之间的差异应该是
代码(退出队列代码)
所述功能包括:
func日历(calendar:JTACMonthView,cellForItemAt日期:日期,cellState:cellState,indexPath:indexPath)
func日历(日历:JTACMonthView,将显示单元格:JTACDayCell,forItemAt-date:date,cellState:cellState,indexPath:indexPath)
向collectionview添加约束,而不仅仅是设置框架。