gpt4 book ai didi

swift - Swift 3.2 中 cellForItemAt 日期的 JTAppleCalendar 自定义单元错误

转载 作者:搜寻专家 更新时间:2023-10-31 08:24:38 28 4
gpt4 key购买 nike

我在我的项目中添加了 JTAppleCalendar,我想在我的一些日历单元格中添加一些标签。我成功添加了它们,但是当我在我的日历月份向左或向右滚动时,标签内的单元格消失、隐藏或混合,当我一次又一次滚动时,混合越来越多。我需要任何协议(protocol)或代表等吗?或者,这只是一个错误?

How can I fix that bug?

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 聊天<​​/strong> https://gitter.im/patchthecode/JTAppleCalendar

最佳答案

基本上,要做出奇怪的“解决方法”,您应该实现经典的UICollectionView(JTAppleCalendar 来自它)willDisplay method ,正如您所见,理论上它应该用于检测单元格添加而不是复制它的内容,因此要重新构建内容,您可以按照同样向 JTAppleCalendar 解释的示例进行操作gitHub issues 由 swift nub 报告 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 中有一个小错误,您可以很容易地纠正它,用 UIButton 替换 DesignableButton(不存在的类),如图所示以避免错误:CalendarBug[9879:1645088] Interface Builder 文件中的未知类 _TtC11CalendarBug16DesignableButton

enter image description here

之后,完整的 JTAppleCaledar 库似乎没有任何问题,事实上作者还扩展了 willDisplay 委托(delegate),解决了单元格渲染的许多问题。

我在 TagListView.swift 类中发现了您的问题,更准确地说是在方法 reset 中。

标签 ListView .swift:

func reset() {
for tag in tags {
tag.removeFromSuperview()
}
tags = []
currentRow = 0
numberOfRows = 0
}

此方法从 superview 中删除所有标签列表(标签数组),但不删除过去添加到 superview 的其他标签,换句话说,仅包含数组 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()} })
}

输出:

enter image description here

关于swift - Swift 3.2 中 cellForItemAt 日期的 JTAppleCalendar 自定义单元错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47485263/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com