Поток 1: EXC_BAD_ACCESS (code = EXC_I386_GPFLT) в UICollectionView? - PullRequest
0 голосов
/ 02 октября 2018

У меня есть UICollectionView

let cellIdentifier = "DayCollectionViewCell"
class ViewController: UIViewController, UICollectionViewDataSource,UICollectionViewDelegate {

    @IBOutlet weak var button: UIButton!
    var dates = [Date?]()
    var startDate: Date?
    @IBOutlet weak var daysCollectionView: UICollectionView!
    override func viewDidLoad() {
        super.viewDidLoad()
        daysCollectionView.register(UINib.init(nibName: "DayCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: cellIdentifier)

        let allDates = Helper.generateRandomDate(daysBack: 900, numberOf: 3)
        self.dates = allDates.sorted(by: {
            $0!.compare($1!) == .orderedAscending
        })
        print(self.dates)
        startDate = self.dates.first! ?? Date()

        daysCollectionView.delegate = self
        daysCollectionView.dataSource = self
        // Do any additional setup after loading the view, typically from a nib.
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 900
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = daysCollectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! DayCollectionViewCell     //ON THIS LINE!!!!! Thread 1: EXC_BAD_ACCESS (code=1, address=0x5b9c38eb4f78)

        let cellDate = Calendar.current.date(byAdding: .day, value: indexPath.item, to: self.startDate!)
        if self.dates.contains(where: { Calendar.current.isDate(cellDate!, inSameDayAs: $0!) }) {
            print("same")
            cell.backgroundColor = UIColor.red
        } else {
            print("not me")
            cell.backgroundColor = UIColor.lightGray
        }
        return cell
    }

}

Затем в другом файле у меня есть

class DayCollectionViewCell: UICollectionViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    func clearArrow(){
        self.layer.sublayers = nil
    }

    func drawArrow(){

        let start_x = self.bounds.minX
        let start_y = self.bounds.minY

        let top_width = self.bounds.width
        let tick_height = self.bounds.height
        let tip_height = CGFloat(10)
        let tip_flare = CGFloat(6)
        let arrowLayer = CAShapeLayer()
        let path = UIBezierPath()
        path.move(to: CGPoint(x: start_x, y: start_y + tick_height))
        path.addLine(to: CGPoint(x: start_x + top_width,y: start_y + tick_height))
        path.addLine(to: CGPoint(x: start_x + top_width + tip_flare,y: start_y+tick_height+tip_height))
        path.addLine(to: CGPoint(x: start_x - tip_flare,y: start_y + tick_height + tip_height))
        path.close()
        arrowLayer.path = path.cgPath
        arrowLayer.fillColor = UIColor(red:0.99, green:0.13, blue:0.25, alpha:1.0).cgColor
        self.layer.addSublayer(arrowLayer)
    }
    override var isSelected: Bool{
        didSet{
            if self.isSelected
            {
               self.drawArrow()
            }
            else
            {
                self.clearArrow()
            }
        }
    }


}

Затем файл DayCollectionViewCell.xib:

enter image description here

Что я здесь не так делаю?

...