问题是当我在选择 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
}
}
}
UITableViewCell:
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)
}
}
让我知道是否需要添加任何其他内容。