0

我正在尝试清除 DateCell ,但在选择包含prepareForReuse时我的实现似乎不起作用。DateCellActivities

我有以下设置:

  1. 仅限 4 月 3 日和 4 月 4 日的虚拟数据
  2. 当有 时,将在日期下方呈现activities一个包含点。stackView

在以下情况下,我的实现工作正常,即正确清理和重用单元格:

  1. dateCells选择否时上下滚动
  2. dateCells选择包含activities时上下滚动

Whenever a dateCellthat contains activitiesare 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()
}

如何清理单元格以使点不会出现在不应该出现的地方?

4

1 回答 1

0

我认为每次重新加载单元格时都需要删除 stackView 。不只是当活动为零时。每次加载一个单元格时,您都会想把石板擦干净。

var activities: [Activity]? {
    didSet {
        stackView.removeFromSuperview()
        if let activities = activities {
            if activities.count > 0 {
                setupStackView(activities)
            }
        }
    }
}
于 2020-04-04T06:15:47.813 回答