我正在尝试清除 DateCell ,但在选择包含prepareForReuse
时我的实现似乎不起作用。DateCell
Activities
我有以下设置:
- 仅限 4 月 3 日和 4 月 4 日的虚拟数据
- 当有 时,将在日期下方呈现
activities
一个包含点。stackView
在以下情况下,我的实现工作正常,即正确清理和重用单元格:
dateCells
选择否时上下滚动dateCells
选择不包含activities
时上下滚动
Whenever a dateCell
that contains activities
are selected, the cells are no longer cleaned up, ie the dots starts appearing at random places. 见动图:
代码如下:
//At viewController: JTACMonthViewDelegate
func configureCell(view: JTACDayCell?, cellState: CellState) {
guard let cell = view as? DateCell else { return }
cell.dateLabel.text = cellState.text
handleCellTextColor(cell: cell, cellState: cellState)
handleCellSelected(cell: cell, cellState: cellState)
highlightTodaysDate(cell: cell, cellState: cellState)
handleCellEvents(cell: cell, cellState: cellState)
}
func highlightTodaysDate(cell: DateCell, cellState: CellState) {
dateFormatter.dateFormat = "YYYY MM dd"
if dateFormatter.string(from: cellState.date) == dateFormatter.string(from: Date()) {
cell.isToday = true
} else {
cell.isToday = false
}
}
func handleCellTextColor(cell: DateCell, cellState: CellState) {
if cellState.dateBelongsTo == .thisMonth {
cell.dateLabel.textColor = UIColor.black
} else {
cell.dateLabel.textColor = UIColor.gray
}
}
func handleCellSelected(cell: DateCell, cellState: CellState) {
if cellState.isSelected {
cell.selectedView.isHidden = false
dateFormatter.dateFormat = "dd-MMM-yyyy"
let dateString = dateFormatter.string(from: cellState.date)
//a separate tableView is in viewController, here getting dataSource for that TV
let filtered = calendarDataSource.filter { $0.key == dateString }
tableViewDataSource = filtered[dateString] ?? []
tableView.reloadData()
} else {
cell.selectedView.isHidden = true
}
}
func handleCellEvents(cell: DateCell, cellState: CellState) {
dateFormatter.dateFormat = "dd-MMM-yyyy"
let dateString = dateFormatter.string(from: cellState.date)
if calendarDataSource[dateString] == nil {
cell.activities = []
} else {
cell.activities = calendarDataSource[dateString]
}
}
//At DateCell
var isToday: Bool? {
didSet {
if isToday ?? false {
//setup red selectedView bg
} else {
//setup gray selectedView bg
}
}
}
var activities: [Activity]? {
didSet {
if let activities = activities {
if activities.count > 0 {
setupStackView(activities)
}
} else {
stackView.removeFromSuperview()
}
}
}
var stackView = UIStackView()
override init(frame: CGRect) {
super.init(frame: frame)
//Other setup work
}
func setupStackView(_ activities: [Activity]) {
var dotViews: [UIView] = activities.map { (activity) -> DotView in
let dotView = DotView()
if activity.type == "1" {
dotView.dotView.backgroundColor = .orange
} else if activity.type == "2" {
dotView.dotView.backgroundColor = .purple
} else {
dotView.dotView.backgroundColor = .lightGray
}
return dotView
}
if activities.count > 1 {
dotViews.insert(UIView(), at: 0)
dotViews.insert(UIView(), at: activities.count + 1)
}
stackView = UIStackView(arrangedSubviews: dotViews)
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .horizontal
stackView.distribution = .fillEqually
addSubview(stackView)
stackView.topAnchor.constraint(equalTo: dateLabel.bottomAnchor).isActive = true
stackView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
stackView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
stackView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
}
override func prepareForReuse() {
super.prepareForReuse()
//Other UI reset work here
stackView.removeFromSuperview()
}
如何清理单元格以使点不会出现在不应该出现的地方?