Swi*_*per 5 calendar swift swift3 jtapplecalendar swift4
我JTAppleCalendar在我的项目中添加了一些标签,我想在我的一些日历单元格中添加一些标签。我成功添加了它们,但是当我在日历月份向左或向右滚动时,标签内的单元格消失、隐藏或混合,并且当我一次又一次滚动时,混合越来越多。我需要任何协议或代表等吗?或者,这只是一个错误?
我该如何修复该错误?
我的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
}
Run Code Online (Sandbox Code Playgroud)
行动中的错误:
https://github.com/LetSwiftDev/CalendarBug/blob/master/calendarbug.gif

你也可以在这里加入官方 JTAppleCalendar 聊天 https://gitter.im/patchthecode/JTAppleCalendar
基本上,要做出一个奇怪的“解决方法”,您应该实现经典的UICollectionView(JTAppleCalendar 来自它)willDisplay 方法,正如您所看到的,理论上它应该用于检测单元格添加而不是复制其内容,所以要使这个重新构建内容,您可以按照本页中还解释了JTAppleCalendar gitHub 问题并由swift nub 报告的示例进行操作。
所以,你的代码可以是:
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..
Run Code Online (Sandbox Code Playgroud)
在您的评论之后,我决定深入分析您的代码。
首先,您的 mainStoryBoard 中有一个小错误,您可以轻松地纠正它,将DesignableButton(不存在的类)替换为UIButton如图所示,以避免错误: CalendarBug[9879:1645088] Unknown class _TtC11CalendarBug16DesignableButton in Interface Builder file。
之后,完整的JTAppleCaledar库似乎没有任何问题,事实上,作者还扩展了willDisplay代表,解决了有关单元格渲染的许多问题。
我在课堂上发现了你的问题TagListView.swift,更准确地说是在方法中reset。
func reset() {
for tag in tags {
tag.removeFromSuperview()
}
tags = []
currentRow = 0
numberOfRows = 0
}
Run Code Online (Sandbox Code Playgroud)
此方法从超级视图中删除所有标签列表(标签数组),但不删除过去添加到超级视图中的其他标签,换句话说,仅删除数组中包含的标签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()} })
}
Run Code Online (Sandbox Code Playgroud)
要优化您的代码,您只需将此方法更正为:
func reset(){
tags = []
currentRow = 0
numberOfRows = 0
self.subviews.forEach({ if $0 is UILabel { $0.removeFromSuperview()} })
}
Run Code Online (Sandbox Code Playgroud)
输出:
| 归档时间: |
|
| 查看次数: |
1562 次 |
| 最近记录: |