上記画像の階層のように、collectionViewの上にtableViewを配置しております。
collectionViewはViewController.swiftにデリゲートを設定しており、
tableViewはCalendarCell.swiftにデリゲートを設定しています。
双方のファイルで同じ変数を共有したいため、AppDelegate.swiftに変数day:Intを定義しています。
collectionViewのscrollViewDidEndDragging内で、
collectionViewがページングされるたびに変数dayの値を変更し、
その値をtableViewのcellForRowAt内で使用したいと思うのですが、
scrollViewDidEndDraggingでの変数dayの値と、
cellForRowAtでの変数dayの値が一致しません。
scrollViewDidEndDragging内では、右へ1ページ進む毎に変数dayの値が1加算され、左に1ページ戻る事に変数dayの値が1減算されます。
cellForRowAtでの変数dayの値もscrollViewDidEndDraggingに合わせたいと思います。
双方のメソッドが呼び出されるタイミングが関係しているのでしょうか。
解決策をご教示頂けますと幸いです。
下記に双方のメソッド内のコードを記述致します。
// ViewController.swift func showDate() { let delegate = UIApplication.shared.delegate as! AppDelegate let seconds = TimeInterval(dayCount*24*60*60) now = NSDate(timeIntervalSinceNow: seconds) as Date let dateFormatter = DateFormatter() dateFormatter.locale = Locale(identifier: "en_US") dateFormatter.dateFormat = "d MMM yyyy" let date1 = dateFormatter.string(from: now as Date) dayTitleLabel.text = date1 let year = calendar.component(.year, from: now as Date) let month = calendar.component(.month, from: now as Date) delegate.day = calendar.component(.day, from: now as Date) let comp = Calendar.Component.weekday let weekday = NSCalendar.current.component(comp, from: now as Date) holidayFlag = holidayObject.judgeJapaneseHoliday(year: year, month: month, day: delegate.day) if holidayFlag == true || weekday == 1 { dayTitleLabel.textColor = UIColor(red: 0.0/255.0, green: 0.0/255.0, blue: 255.0/255.0, alpha: 0.3) } else if weekday == 7 { dayTitleLabel.textColor = UIColor(red: 255.0/255.0, green: 0.0/255.0, blue: 0.0/255.0, alpha: 0.3) } else { dayTitleLabel.textColor = UIColor.black } }
// ViewController.swift func scrollViewWillBeginDragging(_ scrollView: UIScrollView) { scrollBeginingPoint = collectionView.contentOffset; }
// ViewController.swift func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) { var currentPoint = collectionView.contentOffset; if(scrollBeginingPoint.x < currentPoint.x){ dayCount += 1 showDate() }else{ dayCount -= 1 showDate() } }
// CalendarCell.swift func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") let delegate = UIApplication.shared.delegate as! AppDelegate let dateLabel = cell?.viewWithTag(2) as! UILabel let format = DateFormatter() format.dateFormat = "H:mm" delegate.sortedContents = delegate.contents.sorted(by: { $1.startDate>$0.startDate}) let strDate = format.string(from: delegate.sortedContents[indexPath.row].startDate) + ">>>" + format.string(from: delegate.sortedContents[indexPath.row].endDate) dateLabel.text = strDate let label = cell?.viewWithTag(1) as! UILabel label.text = delegate.sortedContents[indexPath.row].title print("day: \(delegate.day)") return cell! }
// ViewController.swift func numberOfSections(in collectionView: UICollectionView) -> Int { return 1 } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return dateManager.daysAcquisition() } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CalendarCell", for: indexPath) as! CalendarCell cell.tableView.reloadData() return cell }
numberOfItemsInSection内で返り値に設定しているdateManager.daysAcquisition()は、DateManager.swiftというファイルに定義している「月ごとのセルの数を出すメソッド」です。