代码如下:
类viewController:uiviewController{
@IBoutlet弱var tblview:uiTableView!
var storedoffsets=[int:cgfloat]()//将由jtappleCalender使用
let formatter=dateformiter()//nsdateformiter
重写func viewdidload()。{
super.viewdidload()。
self.tblview.delegate=自己
self.tblview.datasource=自己
self.tblview.reloaddata()。
}
func handlecell配置(cell:jtapplecell?,电池状态:电池状态){
handlecellselection(视图:单元格,单元格状态:单元格状态)
}
func handlecell选择(视图:jtapplecell?,电池状态:电池状态){
guard let mycustomcell=查看为?日历单元格其他返回
如果选择CellState.is{
mycustomcell.contentview.layer.cornerradius=10
mycustomcell.contentview.backgroundcolor=uicolor.init(红色:0.26,绿色:0.10,蓝色:0.39,alpha:1.0)
}否则{
myCustomCell.ContentView.Layer.CornerRadius=0
mycustomcell.contentview.backgroundcolor=uicolor.clear
}
}
}
< /代码>
uiTableView单元格:
class tablecell:uiTableViewcell{
@iboutlet弱var lbldate:uilabel!
@IBoutlet弱var集合视图日期:jTappleCalendarView!
重写func prepareForReuse()。{
super.PrepareForReuse()。
}
}
扩展表格单元格{
func setcollectionviewdatasourcelegate<d:jtappleCalendarviewdatasource&jtappleCalendarviewdelegate>(u datasourcelegate:d,forrow row:int){
collectionviewDate.calendarDelegate=数据源委派
collectionviewDate.calendardatasource=数据源委托
collectionviewDate.tag=行
collectionviewdate.setContentOffset(collectionviewdate.contentOffset,动画:false)
CollectionViewDate.ReloadData()。
}
var集合视图偏移量:cgfloat{
设置collectionviewdate.contentoffset.x=newvalue_
get返回collectionviewdate.contentoffset.x
}
}
扩展视图控制器:UITableViewDataSource、UITableViewDelegate{
func numberofsections(在TableView:uiTableView中)->int{
返回1
}
func tableview(uTableView:uiTableView,numberofRowsPreagon部分:int)->int{
返回7
}
func tableview(u tableview:uitableview,cellforrowat indexpath:indexpath)->uitableviewcell{
让cell=self.tblview.dequeuerusablecell(withIdentifier:“tablecell”,for:indexpath)作为!表格单元
cell.lbldate.text=“日期”+“\(indexPath.row)”
返回单元
}
func tableview(u tableview:uitableview,willdisplay cell:uitableviewcell,forrowat indexpath:indexpath){
guard let tableViewCell=单元格为?TableCell Else返回
TableViewCell.setCollectionViewDataSourceDelegate(self,forRow:indexPath.row)
tableviewscell.collectionviewoffset=storedoffsets[indexpath.row]?0?
}
func tableview(uTableView:uiTableView,didendDisplaying cell:uiTableViewCell,forrowat indexPath:indexPath){
guard let tableViewCell=单元格为?TableCell Else返回
storedOffsets[indexPath.row]=TableViewCell.CollectionViewOffset
}
}
类CalendarCell:jTappleCell{
@iboutlet弱var lbldate:uilabel!
}
扩展视图控制器:JTappleCalendarViewDataSource、JTappleCalendarViewDelegate{
func日历(日历:jtappleCalendarView,将显示单元格:jtappleCell,Foritemat日期:日期,单元格状态:单元格状态,索引路径:索引路径){
让myCustomCell=单元格为!钙质细胞
sharedFunctionToConfigureCell(myCustomCell:myCustomCell,CellState:CellState,日期:日期)
}
func日历(日历:jTappleCalendarView,cellForItemAt日期:日期,cellstate:cellstate,indexPath:indexPath)->jTappleCell{
让cell=calendar.dequeuersablejtapplecell(带有ReuseIdentifier:“calendarCell”,用于:indexPath)作为!钙质细胞
cell.lbldate.text=单元格状态.text
self.calendar(日历,将显示:单元格,foritemat:日期,单元格状态:单元格状态,索引路径:索引路径)
返回单元
}
func calendar(u calendar:jtappleCalendarView,didDeselectDate日期:日期,单元格:jtappleCell?,电池状态:电池状态){
handlecell配置(cell:cell,cellstate:cellstate)
}
func日历(u calendar:jtappleCalendarView,didselectDate:Date,cell:jtappleCell?,电池状态:电池状态){
handlecell配置(cell:cell,cellstate:cellstate)
}
func sharedfunctiontoconfigureCell(myCustomCell:CalendarCell,CellState:CellState,日期:日期){
handlecell配置(cell:mycustomcell,cellstate:cellstate)
}
func configureCalendar(u calendar:jtappleCalendarView)->配置参数{
让当前日期=日期()。
让enddate=calendar.current.date(通过添加:.month,value:4,to:date())
让参数=配置参数(开始日期:当前日期,
结束日期:结束日期!,
行数:1,
生成日期:.第一个月,
GenerateOutdates:。关闭,
hasstrictboundaries:错误)
calendar.scrollToDate(当前日期,triggerscrollToDateDelegate:false,animatescroll:false)
返回参数
}
func configurevisibleCell(myCustomCell:CalendarCell,CellState:CellState,日期:日期){
handlecell配置(cell:mycustomcell,cellstate:cellstate)
}
}
< /代码>
如果需要添加其他内容,请通知我。
问题是当我在选择CollectionViewCell(日期28为紫色)后滚动TableView时。在TableView底部的一些CollectionViewCells似乎被选中(此处,CollectionView位于TableView内部)。
代码如下:
class ViewController: UIViewController {
@IBOutlet weak var tblView: UITableView!
var storedOffsets = [Int: CGFloat]() // Will be used by JTAppleCalender
let formatter = DateFormatter() // NSDateFormatter
override func viewDidLoad() {
super.viewDidLoad()
self.tblView.delegate = self
self.tblView.dataSource = self
self.tblView.reloadData()
}
func handleCellConfiguration(cell: JTAppleCell?, cellState: CellState) {
handleCellSelection(view: cell, cellState: cellState)
}
func handleCellSelection(view: JTAppleCell?, cellState: CellState) {
guard let myCustomCell = view as? CalenderCell else {return }
if cellState.isSelected {
myCustomCell.contentView.layer.cornerRadius = 10
myCustomCell.contentView.backgroundColor = UIColor.init(red: 0.26, green: 0.10, blue: 0.39, alpha: 1.0)
} else {
myCustomCell.contentView.layer.cornerRadius = 0
myCustomCell.contentView.backgroundColor = UIColor.clear
}
}
}
uiTableView单元格:
class TableCell: UITableViewCell{
@IBOutlet weak var lbldate: UILabel!
@IBOutlet weak var collectionViewDate: JTAppleCalendarView!
override func prepareForReuse() {
super.prepareForReuse()
}
}
extension TableCell {
func setCollectionViewDataSourceDelegate<D: JTAppleCalendarViewDataSource & JTAppleCalendarViewDelegate>(_ dataSourceDelegate: D, forRow row: Int) {
collectionViewDate.calendarDelegate = dataSourceDelegate
collectionViewDate.calendarDataSource = dataSourceDelegate
collectionViewDate.tag = row
collectionViewDate.setContentOffset(collectionViewDate.contentOffset, animated:false)
collectionViewDate.reloadData()
}
var collectionViewOffset: CGFloat {
set { collectionViewDate.contentOffset.x = newValue }
get { return collectionViewDate.contentOffset.x }
}
}
extension ViewController : UITableViewDataSource, UITableViewDelegate{
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 7
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tblView.dequeueReusableCell(withIdentifier: "TableCell", for: indexPath) as! TableCell
cell.lbldate.text = "date "+"\(indexPath.row)"
return cell
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
guard let tableViewCell = cell as? TableCell else { return }
tableViewCell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)
tableViewCell.collectionViewOffset = storedOffsets[indexPath.row] ?? 0
}
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
guard let tableViewCell = cell as? TableCell else { return }
storedOffsets[indexPath.row] = tableViewCell.collectionViewOffset
}
}
class CalenderCell: JTAppleCell{
@IBOutlet weak var lblDate: UILabel!
}
extension ViewController: JTAppleCalendarViewDataSource , JTAppleCalendarViewDelegate {
func calendar(_ calendar: JTAppleCalendarView, willDisplay cell: JTAppleCell, forItemAt date: Date, cellState: CellState, indexPath: IndexPath) {
let myCustomCell = cell as! CalenderCell
sharedFunctionToConfigureCell(myCustomCell: myCustomCell, cellState: cellState, date: date)
}
func calendar(_ calendar: JTAppleCalendarView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell {
let cell = calendar.dequeueReusableJTAppleCell(withReuseIdentifier: "CalenderCell", for: indexPath) as! CalenderCell
cell.lblDate.text = cellState.text
self.calendar(calendar, willDisplay: cell, forItemAt: date, cellState: cellState, indexPath: indexPath)
return cell
}
func calendar(_ calendar: JTAppleCalendarView, didDeselectDate date: Date, cell: JTAppleCell?, cellState: CellState) {
handleCellConfiguration(cell: cell, cellState: cellState)
}
func calendar(_ calendar: JTAppleCalendarView, didSelectDate date: Date, cell: JTAppleCell?, cellState: CellState) {
handleCellConfiguration(cell: cell, cellState: cellState)
}
func sharedFunctionToConfigureCell(myCustomCell: CalenderCell, cellState: CellState, date: Date) {
handleCellConfiguration(cell: myCustomCell, cellState: cellState)
}
func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters {
let currentDate = Date()
let endDate = Calendar.current.date(byAdding: .month, value: 4, to: Date())
let parameters = ConfigurationParameters(startDate: currentDate,
endDate: endDate!,
numberOfRows: 1,
generateInDates: .forFirstMonthOnly,
generateOutDates: .off,
hasStrictBoundaries: false)
calendar.scrollToDate(currentDate, triggerScrollToDateDelegate: false, animateScroll: false)
return parameters
}
func configureVisibleCell(myCustomCell: CalenderCell, cellState: CellState, date: Date) {
handleCellConfiguration(cell: myCustomCell, cellState: cellState)
}
}
如果需要添加其他内容,请通知我。