解決UICollectionView的Cell復(fù)用引起的布局混亂問題

//

//? ZHMonthCourseView.swift

//? ZHChat

//

//? Created by Liujc on 2020/10/29.

//? Copyright ? 2020 奇異的炸醬面. All rights reserved.

//

importFoundation

importUIKit

importCNSCommon

let kMonthRange = 12

protocol CalendarViewDelegate: class {

? ? funcdidSelectDate(date:Date)

}

class ZHMonthCourseView: UIView {


? ? weak var delegate: CalendarViewDelegate?

? ? varweek: [String] = ["日","一","二","三","四","五","六"]

? ? let clv = UICollectionView(frame: CGRect.zero, collectionViewLayout: UICollectionViewFlowLayout())

? ? private var collectionData = [CalendarLogic]()

? ? privatevardaraArray: [CalendarLogic] = []

? ? vardates= [Date]()

? ? var previousMonthVisibleDatesCount = 0

? ? var currentMonthVisibleDatesCount = 0

? ? var nextMonthVisibleDatesCount = 0

? ? var dataContentArr :[ZHWorkScheduleModle] = []

? ? varidentifierdic:[String:String] = [:]

? ? var logic: CalendarLogic? {

? ? ? ? didSet{

? ? ? ? ? ? populateDates()

? ? ? ? ? ? clv.reloadData()

? ? ? ? }

? ? }


? ? //? ? ? ? var selectedDate: Date? {

? ? //? ? ? ? ? ? didSet {

? ? //? ? ? ? ? ? ? ? if clv != nil {

? ? //? ? ? ? ? ? ? ? ? ? clv.reloadData()

? ? //? ? ? ? ? ? ? ? }

? ? //? ? ? ? ? ? }

? ? //? ? ? ? }


? ? func populateDates() {

? ? ? ? iflogic!=nil{

? ? ? ? ? ? dates= [Date]()


? ? ? ? ? ? dates += logic!.previousMonthVisibleDays!

? ? ? ? ? ? dates += logic!.currentMonthDays!

? ? ? ? ? ? dates += logic!.nextMonthVisibleDays!


? ? ? ? ? ? previousMonthVisibleDatesCount = logic!.previousMonthVisibleDays!.count

? ? ? ? ? ? currentMonthVisibleDatesCount = logic!.currentMonthDays!.count

? ? ? ? ? ? nextMonthVisibleDatesCount = logic!.nextMonthVisibleDays!.count


? ? ? ? }else{

? ? ? ? ? ? dates.removeAll(keepingCapacity:false)

? ? ? ? }


? ? }

? ? varbaseDate:Date? {

? ? ? ? didSet{


? ? ? ? ? ? collectionData= [CalendarLogic]()

? ? ? ? ? ? ifletdate =baseDate{

? ? ? ? ? ? ? ? vardateIter1 = date, dateIter2 = date


? ? ? ? ? ? ? ? varset =Set()

? ? ? ? ? ? ? ? set.insert(CalendarLogic(date:baseDate!))


? ? ? ? ? ? ? ? // advance one year

? ? ? ? ? ? ? ? (0..<kMonthRange).forEach{_in

? ? ? ? ? ? ? ? ? ? dateIter1 = dateIter1.firstDayOfFollowingMonth

? ? ? ? ? ? ? ? ? ? dateIter2 = dateIter2.firstDayOfPreviousMonth


? ? ? ? ? ? ? ? ? ? set.insert(CalendarLogic(date: dateIter1))

? ? ? ? ? ? ? ? ? ? set.insert(CalendarLogic(date: dateIter2))

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? collectionData=Array(set).sorted(by:<)

? ? ? ? ? ? }

? ? ? ? ? ? foritemincollectionData{

? ? ? ? ? ? ? ? print("&&&&&&\(String(describing: item.currentMonthAndYear))")

? ? ? ? ? ? }

? ? ? ? ? ? self.logic = collectionData[12]

? ? ? ? ? ? clv.reloadData()


? ? ? ? }

? ? }


? ? var selectedDate: Date? {

? ? ? ? didSet{


? ? ? ? ? ? self.clv.reloadData()

? ? ? ? ? ? DispatchQueue.main.async{

? ? ? ? ? ? ? ? ifself.delegate!=nil{

? ? ? ? ? ? ? ? ? ? self.delegate!.didSelectDate(date:self.selectedDate!)

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }


? ? ? ? }

? ? }



? ? overrideinit(frame:CGRect) {

? ? ? ? super.init(frame: frame)

? ? }


? ? convenienceinit(frame:CGRect,baseDate:Date, selectedDate:Date) {

? ? ? ? self.init(frame: frame)

? ? ? ? self.baseDate= baseDate

? ? ? ? self.selectedDate= selectedDate

? ? ? ? self.backgroundColor = .white

?? ? ? // clv.register(ZHDayCourseCell.classForCoder(), forCellWithReuseIdentifier: "ZHDayCourseCell")

? ? ? ? clv.register(ZHWeekHeaderCell.classForCoder(), forSupplementaryViewOfKind:UICollectionElementKindSectionHeader, withReuseIdentifier:"ZHWeekHeaderCell")

? ? ? ? clv.delegate=self

? ? ? ? clv.dataSource=self

? ? ? ? clv.backgroundColor = .white

? ? ? ? self.clv.layer.borderWidth = 0.5

? ? ? ? self.clv.layer.borderColor = UIColor.init(228,235,243).cgColor

? ? ? ? clv.frame=CGRect.init(x:0, y:0, width:CNDevice.screenWidth, height:CNDevice.screenHeight-CNDevice.navBarHeight-72)

? ? ? ? self.addSubview(self.clv)

? ? }



? ? requiredinit?(coder aDecoder:NSCoder) {super.init(coder: aDecoder) }


? ? func refreshData(){



? ? }

}

extension ZHMonthCourseView:UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{

? ? funccollectionView(_collectionView:UICollectionView, numberOfItemsInSection section:Int) ->Int{

? ? ? ? // 7*6 = 42 :- 7 columns (7 days in a week) and 6 rows (max 6 weeks in a month)

? ? ? ? return42

? ? }


? ? funccollectionView(_collectionView:UICollectionView, cellForItemAt indexPath:IndexPath) ->UICollectionViewCell{


? ? ? ? varid =identifierdic["\(indexPath)"].toString() ??""

? ? ? ? ifid==""{

? ? ? ? ? ? id ="ZHDayCourseCell"+"\(indexPath)"

? ? ? ? ? ? identifierdic["\(indexPath)"] = id

? ? ? ? ? ? collectionView.register(ZHDayCourseCell.classForCoder(), forCellWithReuseIdentifier: id)


? ? ? ? }



? ? ? ? letcell = collectionView.dequeueReusableCell(withReuseIdentifier: id,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? for: indexPath)as!ZHDayCourseCell


? ? ? ? letdate =dates[indexPath.item]

? ? ? ? cell.date= (indexPath.item<dates.count) ? date :nil

? ? ? ? cell.mark= (selectedDate==date)

? ? ? ? cell.disabled = (indexPath.item < previousMonthVisibleDatesCount) ||

? ? ? ? ? ? (indexPath.item >= previousMonthVisibleDatesCount

? ? ? ? ? ? ? ? + currentMonthVisibleDatesCount)

? ? ? ? letcaltimestr =? self.WorkTimedateString(date)


? ? //? ? titleArray = ["hello","world","hello","world","hello"]

? ? ? ? if? !cell.disabled{//當(dāng)月才給賦值

?? ? ? ? ? ? let? titleArray =? ZHWorkScheduleModle.getTitleArray(DataArray:self.dataContentArr,clendartitle:caltimestr )

?? ? ? ? ? ? cell.getTitle(titleArr: titleArray)

? ? ? ? }



? ? ? ? print("^^^^^^^^^^\(date)&&&&$$$$$$\(caltimestr)*****\(date.day)*****")

? ? ? ? returncell

? ? }



? ? funccollectionView(_collectionView:UICollectionView,

? ? ? ? ? ? ? ? ? ? ? ? didSelectItemAt indexPath:IndexPath) {

? ? ? ? letdate =dates[indexPath.item]

? ? ? ? letcaltimestr =? self.WorkTimedateString(date)

? ? ? ? lettitleArray =? ZHWorkScheduleModle.getTitleArray(DataArray:self.dataContentArr,clendartitle:caltimestr )

? ? ? ? iftitleArray.count>0{

? ? ? ? ? ? letvc =? ZHWorkDetailViewController.init()

? ? ? ? ? ? vc.dateStr= caltimestr

? ? ? ? ? ? self.navigationController?.pushViewController(vc, animated:true)

? ? ? ? }


? ? }


? ? funccollectionView(_collectionView:UICollectionView,

? ? ? ? ? ? ? ? ? ? ? ? viewForSupplementaryElementOfKind kind:String,

? ? ? ? ? ? ? ? ? ? ? ? at indexPath:IndexPath) ->UICollectionReusableView{

? ? ? ? switchkind {

? ? ? ? case UICollectionElementKindSectionHeader:

? ? ? ? ? ? letheaderView = collectionView.dequeueReusableSupplementaryView(ofKind:UICollectionElementKindSectionHeader,? withReuseIdentifier:"ZHWeekHeaderCell", for: indexPath)

? ? ? ? ? ? returnheaderView

? ? ? ? default:

? ? ? ? ? ? return UICollectionReusableView.init(frame: CGRect.zero)

? ? ? ? }

? ? }


? ? funccollectionView(_collectionView:UICollectionView,

? ? ? ? ? ? ? ? ? ? ? ? layout collectionViewLayout:UICollectionViewLayout,

? ? ? ? ? ? ? ? ? ? ? ? sizeForItemAt indexPath:IndexPath) ->CGSize{

? ? ? ? returnCGSize(width: collectionView.frame.width/7.0, height: collectionView.frame.height/7.0)

? ? }


? ? funccollectionView(_collectionView:UICollectionView,

? ? ? ? ? ? ? ? ? ? ? ? layout collectionViewLayout:UICollectionViewLayout,

? ? ? ? ? ? ? ? ? ? ? ? referenceSizeForHeaderInSection section:Int) ->CGSize{

? ? ? ? returnCGSize(width: collectionView.frame.width, height:40)

? ? }

? ? funccollectionView(_collectionView:UICollectionView, layout collectionViewLayout:UICollectionViewLayout, insetForSectionAt section:Int) ->UIEdgeInsets{

? ? ? ? returnUIEdgeInsets.init(top:0, left:0, bottom:0, right:0)

? ? }


? ? funccollectionView(_collectionView:UICollectionView, layout collectionViewLayout:UICollectionViewLayout, minimumLineSpacingForSectionAt section:Int) ->CGFloat{

? ? ? ? return0

? ? }


? ? funccollectionView(_collectionView:UICollectionView, layout collectionViewLayout:UICollectionViewLayout, minimumInteritemSpacingForSectionAt section:Int) ->CGFloat{

? ? ? ? return0

? ? }

}

extension ZHMonthCourseView:MonthCollectionCellDelegate{


? ? //MARK: Month cell delegate.

? ? funcdidSelect(date:Date) {

? ? ? ? selectedDate= date

? ? }


}

extension ZHMonthCourseView {

? ? funcWorkTimedateString(_date:Date) ->String{

?? ? ? ? letformatter =DateFormatter()

?? ? ? ? formatter.locale=Locale.init(identifier:"zh_CN")

?? ? ? ? formatter.dateFormat="yyyy-MM-dd"

?? ? ? ? letdate = formatter.string(from: date)

?? ? ? ? returndate

?? ? }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容