###前提・実現したいこと
swift3,xcode8.2.1にて作業をしています。
以下こちらの記事を参考させていただきました。
https://teratail.com/questions/61968
http://docs.fabo.io/swift/uikit/054_customuicollectionview.html
文字数の関係で必要ないだろうと思われるところは略しています。
1画面中に3つのcollectionViewをscrollViewに横並びに設置して
先月、今月、来月の順で表示ができるカレンダーを作っています。
実際には常に今月のみが表示されていて、その他前後の月はスワイプすることで表示されます。
来月にスワイプで切り替わったきったときに今月を表示させつつ各月を次の月にreloadDateさせてます。
先月の場合はその逆です。
###発生している問題・エラーメッセージ
スワイプ時の月によってクラッシュします。
月によって行数が変わるのですが、適切な数字を入れてもなぜか該当月にクラッシュします。
2017-02-02 13:12:20.628 CalendarScrollApp[13353:574951] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView received layout attributes for a cell with an index path that does not exist: <NSIndexPath: 0xc000000004600116> {length = 2, path = 1 - 35}' (lldb)
といったメッセージがコンソールに表示されます。
###該当のソースコード
基本的にこちらの記事を参考させていただきました。
https://teratail.com/questions/61968
http://docs.fabo.io/swift/uikit/054_customuicollectionview.html
文字数の関係で必要ないだろうと思われるところは略しています。
上記以外の試みとして、
scrollViewを設置しその中にcollectionViewを置いています。
スワイプ処理以下のようにしています。
swift
1 func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { 2 let w = UIScreen.main.bounds.width 3 let x = scrollView.contentOffset.x 4 var frag: Int! = 0 5 6 switch x { 7 case 0..<w: 8 frag = 1 9 break 10 case (w * 2)..<(w * 3): 11 frag = 2 12 break 13 default: 14 frag = 0 15 } 16 17 if frag == 1 { 18 selectedDatePrev = dateManagerPrev.prevMonth(date: selectedDatePrev as Date) as NSDate 19 selectedDate = dateManagerTo.prevMonth(date: selectedDate as Date) as NSDate 20 selectedDateNext = dateManagerNext.prevMonth(date: selectedDateNext as Date) as NSDate 21 } else if frag == 2 { 22 selectedDatePrev = dateManagerPrev.nextMonth(date: selectedDatePrev as Date) as NSDate 23 selectedDate = dateManagerTo.nextMonth(date: selectedDate as Date) as NSDate 24 selectedDateNext = dateManagerNext.nextMonth(date: selectedDateNext as Date) as NSDate 25 } 26 if frag != 0 { 27 calenderCollectionViewPrev.reloadData() 28 calenderCollectionViewTo.reloadData() 29 calenderCollectionViewNext.reloadData() 30 headerLabel.text = changeHeaderTitle(date: selectedDate as Date) 31 headerLabel.sizeToFit() 32 headerLabel.center = CGPoint(x: headerView.center.x, y: headerView.center.y/2) 33 34 scrollView.contentOffset.x = w 35 } 36 }
###試したこと
ステップ実行でnumberOfItemsInSectionでcell数を渡すタイミングでクラッシュしているところまではわかったのですが、、、それ以上がどうしてもわかりません。
swift
1 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 2 if collectionView == calenderCollectionViewTo { 3 return dateManagerTo.daysAcquisition() 4 } else if collectionView == calenderCollectionViewNext { 5 return dateManagerNext.daysAcquisition() 6 } else if collectionView == calenderCollectionViewPrev { 7 return dateManagerPrev.daysAcquisition() 8 } else { 9 return 0 10 } 11 }
collectionViewをインスタンス化した順番によって変わりますがいずれかをreturnした時にクラッシュします。
数字の渡し方が悪いのか全く見当違いのことをしているのか、
どうかアドバイスを頂けますと幸いです。
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー