5

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

我该如何修复该错误?

我的示例 GitHub 项目

我的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

4

2 回答 2

4

基本上,要制作一个奇怪的“解决方法”,您应该实现经典的UICollectionView(JTAppleCalendar 来自它)willDisplay 方法,正如您所看到的,理论上它应该用于检测单元格添加而不是复制它的内容,所以要做到这一点您可以按照示例重新构建内容,该示例还解释了JTAppleCalendar gitHub 问题并由swift nub在页面中报告。

因此,您的代码可能是:

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代理,解决了许多关于渲染单元格的问题。

我在TagListView.swift课堂上发现了你的问题,更准确地说是在方法中reset

TagListView.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()} })
}

输出

在此处输入图像描述

于 2017-11-28T11:07:59.600 回答
1

我不知道这是否是真正的问题,但你错过了一个功能。实现willDisplayCell这里所示的功能-> https://github.com/patchthecode/JTAppleCalendar/issues/553

这可能会解决您的问题。但奇怪的是,您的自定义框架没有警告您忘记实现该功能。

于 2017-11-25T15:11:22.920 回答