我在项目中添加了JTAppleCalendar
,并且希望在某些日历单元格中添加一些标签。我成功添加了它们,但是当我在日历月中向左或向右滚动时,标记内的单元格消失,隐藏或混合,并且当我一次又一次滚动时,混合越来越多。我需要任何协议(protocol)或委托(delegate)等吗?或者,这仅仅是一个错误?
My example GitHub project
我的cellForItemAt
代码:
func calendar(_ calendar: JTAppleCalendarView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell {
let cell = calendar.dequeueReusableCell(withReuseIdentifier: "CellView", for: indexPath) as! CellView
var currentdate = String(describing: myCalendar.date(byAdding: .day, value: 1, to: cellState.date))
currentdate = currentdate.substring(from: 9, length: 10)
cell.tagList.tags.removeAll()
cell.tagList.hide()
cell.contentView.backgroundColor = nil
cell.tagList.alpha = 0
cell.tagList.numberOfRows = 0
cell.tagList.backgroundColor = UIColor.clear
cell.tagList.isHidden = true
var i : Int
i = 0
for object in datas {
i = i + 1
let clean = "\(object)".components(separatedBy: "*")
if clean[0] == currentdate {
let gotag : Int
gotag = Int(clean[1])!
cell.tagList.isHidden = false
cell.dayLabel.text = cellState.text
cell.contentView.backgroundColor = UIColor.gray
let itemName = "Item name \(i)"
cell.tagList.alpha = 1
if clean[1] == "1" {
cell.tagList.addTag(itemName, target: self, tapAction: #selector(ViewController.tap(_:)), longPressAction: #selector(ViewController.tap(_:)),backgroundColor: UIColor.orange,textColor: UIColor.white,comesTag: gotag)
}else if clean[1] == "2" {
cell.tagList.addTag(itemName, target: self, tapAction: #selector(ViewController.tap(_:)), longPressAction: #selector(ViewController.tap(_:)),backgroundColor: UIColor.green,textColor: UIColor.white,comesTag: gotag)
}else if clean[1] == "3" {
cell.tagList.addTag(itemName, target: self, tapAction: #selector(ViewController.tap(_:)), longPressAction: #selector(ViewController.tap(_:)),backgroundColor: UIColor.brown,textColor: UIColor.white,comesTag: gotag)
}else if clean[1] == "4" {
cell.tagList.addTag(itemName, target: self, tapAction: #selector(ViewController.tap(_:)), longPressAction: #selector(ViewController.tap(_:)),backgroundColor: UIColor.black,textColor: UIColor.white,comesTag: gotag)
}
}else{
cell.tagList.backgroundColor = UIColor.clear
}
}
handleCellConfiguration(cell: cell, cellState: cellState)
return cell
}
实际发生的错误:
https://github.com/LetSwiftDev/CalendarBug/blob/master/calendarbug.gif
也可以在这里加入官方的JTAppleCalendar聊天
https://gitter.im/patchthecode/JTAppleCalendar
最佳答案
基本上,要做出一个怪异的“解决方法”,您应该实现经典的 UICollectionView (JTAppleCalendar源自它)willDisplay
method,如您所见,从理论上讲,它应该用于检测单元格的添加而不是复制其内容,因此要重新构建内容,您可以按照示例进行操作,该示例也向swt nub gitHub在此页面中报告了 JTAppleCalendar issues here和进行了说明。
因此,您的代码可能是:
ViewController.swift :extension ViewController: JTAppleCalendarViewDelegate, JTAppleCalendarViewDataSource {
func calendar(_ calendar: JTAppleCalendarView, willDisplay cell: JTAppleCell, forItemAt date: Date, cellState: CellState, indexPath: IndexPath) {
var cell = cell as! CellView
cell = sharedFunctionToConfigureCell(cell: cell, cellState: cellState, date: date)
}
func calendar(_ calendar: JTAppleCalendarView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell {
var cell = calendar.dequeueReusableCell(withReuseIdentifier: "CellView", for: indexPath) as! CellView
cell = sharedFunctionToConfigureCell(cell: cell, cellState: cellState, date: date)
return cell
}
func sharedFunctionToConfigureCell(cell: CellView, cellState: CellState, date: Date)-> CellView {
var currentdate = String(describing: myCalendar.date(byAdding: .day, value: 1, to: cellState.date))
currentdate = currentdate.substring(from: 9, length: 10)
cell.tagList.tags.removeAll()
cell.tagList.hide()
cell.contentView.backgroundColor = nil
cell.tagList.alpha = 0
cell.tagList.numberOfRows = 0
cell.tagList.backgroundColor = UIColor.clear
cell.tagList.isHidden = true
var i : Int
i = 0
for object in datas {
i = i + 1
let clean = "\(object)".components(separatedBy: "*")
if clean[0] == currentdate {
let gotag : Int
gotag = Int(clean[1])!
cell.tagList.isHidden = false
cell.dayLabel.text = cellState.text
cell.contentView.backgroundColor = UIColor.gray
let itemName = "Item name \(i)"
cell.tagList.alpha = 1
if clean[1] == "1" {
cell.tagList.addTag(itemName, target: self, tapAction: #selector(ViewController.tap(_:)), longPressAction: #selector(ViewController.tap(_:)),backgroundColor: UIColor.orange,textColor: UIColor.white,comesTag: gotag)
}else if clean[1] == "2" {
cell.tagList.addTag(itemName, target: self, tapAction: #selector(ViewController.tap(_:)), longPressAction: #selector(ViewController.tap(_:)),backgroundColor: UIColor.green,textColor: UIColor.white,comesTag: gotag)
}else if clean[1] == "3" {
cell.tagList.addTag(itemName, target: self, tapAction: #selector(ViewController.tap(_:)), longPressAction: #selector(ViewController.tap(_:)),backgroundColor: UIColor.brown,textColor: UIColor.white,comesTag: gotag)
}else if clean[1] == "4" {
cell.tagList.addTag(itemName, target: self, tapAction: #selector(ViewController.tap(_:)), longPressAction: #selector(ViewController.tap(_:)),backgroundColor: UIColor.black,textColor: UIColor.white,comesTag: gotag)
}
}else{
cell.tagList.backgroundColor = UIColor.clear
}
}
handleCellConfiguration(cell: cell, cellState: cellState)
return cell
}
// your other code..
更新(测试后):
在您发表评论之后,我决定深入分析您的代码。
首先,在mainStoryBoard中有一个小错误,您可以轻松地更正它,如下面的图片所示,用DesignableButton
替换UIButton
(不存在的类),以避免错误: CalendarBug[9879:1645088] Unknown class _TtC11CalendarBug16DesignableButton in Interface Builder file
。
之后,完整的JTAppleCaledar
库似乎没有任何问题,实际上,作者还扩展了willDisplay
委托(delegate),该委托(delegate)解决了有关单元格渲染的许多问题。
我已经在TagListView.swift
类中找到了您的问题,更确切地说是在reset
方法中找到了您的问题。
TagListView.swift:func reset() {
for tag in tags {
tag.removeFromSuperview()
}
tags = []
currentRow = 0
numberOfRows = 0
}
此方法从 super View 中删除所有标签列表(标签数组),但不删除过去添加到 super View 中的其他标签,换句话说,仅删除数组tags
中包含的标签。因此,为避免此问题,您可以通过在线添加来增强reset
方法(我们知道它们是UILabel
,因此不需要知道所有的tag
号):func reset() {
for tag in tags {
tag.removeFromSuperview()
}
tags = []
currentRow = 0
numberOfRows = 0
self.subviews.forEach({ if $0 is UILabel { $0.removeFromSuperview()} })
}
要优化您的代码,只需将此方法更正为:func reset(){
tags = []
currentRow = 0
numberOfRows = 0
self.subviews.forEach({ if $0 is UILabel { $0.removeFromSuperview()} })
}
输出: