UICollectionView中的可选值

时间:2016-04-25 10:49:41

标签: ios swift uicollectionview optional

  • 我的问题:

请阅读我的说明:

尝试像iOS的Native Calendar APP一样编写模型。

与以往一样,简单的逻辑:12个月= 12个部分,month.days.count =部分中的单元格数...

在与Date Calculation和UICollectionView斗争之后,这个小模型几乎完成了。

只有一个Unexpectedly found nil while unwrapping错误让我发疯。

  • 如何触发此错误?

在这里:我们在日历应用程序中选择特定日期,我们选择的日期将突出显示或将有明显改变。

例如,如果我选择2016年1月1日,则会突出显示Jan 1st 2016 Cell。然后我选择2016年1月2日(相对较短的时间段),Jan 1st 2016 Cell将切换到正常状态并突出显示Jan 2nd 2016 Cell

目前,没有触发任何错误。

但是,如果我在选择Dec 1st 2016后相对较长时间选择Jan 2nd 2016。该应用程序因致命错误而崩溃。

  • 我们可以知道

    如果我们选择了两个相对较长的日期,那么App崩溃了。但是应用程序运行良好,选择了较短的相对时段。

  • 我们不知道

    两个选定日期之间的相对时间有多长?

    哪个nil-lovely变量让App崩溃了?

  • 守则:

viewDidLoad中():

    override func viewDidLoad() {
    super.viewDidLoad()
    // Create the dateUnitArr which includes dates of Year 2016, Days of Year 2016 seperated in 12 Months
    for indexOfMonth in 1...12 {
        let numOfEmpty = self.numOfEmptyCell(HeadDayOfMonth: NSDate(year: 2016, month: indexOfMonth, day: 1))
        let numOfMonth = NSDate(year: 2016, month: indexOfMonth, day: 1).monthDays
        self.dateUnitArr.append([NSDate](count: numOfEmpty + numOfMonth, repeatedValue: NSDate()))

        self.selectedUnitArr.append([Bool](count: numOfEmpty + numOfMonth, repeatedValue: false))
    }
    // Allocate values to Days in the dateUnitArr
    for indexOfMonth in 0...11 {
        let numOfEmpty = self.numOfEmptyCell(HeadDayOfMonth: NSDate(year: 2016, month: indexOfMonth+1, day: 1))
        let numOfDays = NSDate(year: 2016, month: indexOfMonth + 1, day: 1).monthDays + numOfEmpty
        if numOfEmpty == 0 {
            for indexOfDay in 0...numOfDays - 1 {
                self.dateUnitArr[indexOfMonth][indexOfDay] = NSDate(year: 2016, month: indexOfMonth + 1, day: indexOfDay + 1)
            }
        } else {
            for indexOfDay in 0...numOfDays - 1 {
                if indexOfDay < numOfEmpty {
                    self.dateUnitArr[indexOfMonth][indexOfDay] = NSDate(year: 2012, month: 1, day: 1)
                } else {
                    self.dateUnitArr[indexOfMonth][indexOfDay] = NSDate(year: 2016, month: indexOfMonth + 1, day: indexOfDay + 1 - numOfEmpty)
                }
            }
        }
    }
}

cellForItemAtIndexPath():

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("DateCell", forIndexPath: indexPath)
        as! CollectionViewCell
    if self.dateUnitArr[indexPath.section][indexPath.row] < NSDate(year: 2015, month: 1, day: 1) {
        cell.strOfMonth.text = ""
        cell.numOfDay.text = ""
        cell.numOfDay.backgroundColor = UIColor.whiteColor()
        cell.numOfDay.textColor = UIColor.whiteColor()
        cell.numOfDay.layer.cornerRadius = 0
        cell.numOfDay.clipsToBounds = false
        cell.userInteractionEnabled = false
    } else {
        if self.isFirstDayOfMonth(InputDate: self.dateUnitArr[indexPath.section][indexPath.row]) {
            let dateOfCell = self.dateUnitArr[indexPath.section][indexPath.row]
            let monthOfCell = dateOfCell.monthName
            let index: String.Index = monthOfCell.startIndex.advancedBy(3)
            let shortedMonth = monthOfCell.substringToIndex(index)
            cell.strOfMonth.text = shortedMonth
            cell.numOfDay.text = self.dateUnitArr[indexPath.section][indexPath.row].toString(DateFormat.Custom("dd"))
            cell.userInteractionEnabled = true

            if self.selectedUnitArr[indexPath.section][indexPath.row] {
                cell.numOfDay.backgroundColor = UIColor.darkGrayColor()
                cell.numOfDay.textColor = UIColor.whiteColor()
                cell.numOfDay.layer.cornerRadius = 15
                cell.numOfDay.clipsToBounds = true
            } else {
                cell.numOfDay.backgroundColor = UIColor.whiteColor()
                cell.numOfDay.textColor = UIColor.blackColor()
                cell.numOfDay.layer.cornerRadius = 0
                cell.numOfDay.clipsToBounds = false
            }
        } else {
            cell.strOfMonth.text = ""
            cell.numOfDay.text = self.dateUnitArr[indexPath.section][indexPath.row].toString(DateFormat.Custom("dd"))
            cell.userInteractionEnabled = true
            if self.selectedUnitArr[indexPath.section][indexPath.row] {
                cell.numOfDay.backgroundColor = UIColor.darkGrayColor()
                cell.numOfDay.textColor = UIColor.whiteColor()
                cell.numOfDay.layer.cornerRadius = 15
                cell.numOfDay.clipsToBounds = true
            } else {
                cell.numOfDay.backgroundColor = UIColor.whiteColor()
                cell.numOfDay.textColor = UIColor.blackColor()
                cell.numOfDay.layer.cornerRadius = 0
                cell.numOfDay.clipsToBounds = false
            }
        }
    }
    return cell
}

didSelctedItemAtIndexPath():这里的错误触发器

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    print(self.dateUnitArr[indexPath.section][indexPath.row] + 1.days)
    if self.isSelected {
        // Unmark the SelectedCell
        self.selectedUnitArr[self.selectedIndex.section][self.selectedIndex.row] = false
             let selectedCell = collectionView.cellForItemAtIndexPath(self.selectedIndex) as! CollectionViewCell 
        selectedCell.numOfDay.backgroundColor = UIColor.whiteColor()
        selectedCell.numOfDay.textColor = UIColor.blackColor()
        selectedCell.numOfDay.layer.cornerRadius = 0
        selectedCell.numOfDay.clipsToBounds = false
        // Mark the Neo SelectedCell
        self.selectedIndex = NSIndexPath(forRow: indexPath.row, inSection: indexPath.section)
        self.selectedUnitArr[indexPath.section][indexPath.row] = true
        let cell = collectionView.cellForItemAtIndexPath(indexPath) as! CollectionViewCell
        cell.numOfDay.backgroundColor = UIColor.darkGrayColor()
        cell.numOfDay.textColor = UIColor.whiteColor()
        cell.numOfDay.layer.cornerRadius = 15
        cell.numOfDay.clipsToBounds = true
        self.collectView.reloadData()
    } else {
        // Mark the Neo SelectedCell
        self.isSelected = true
        self.selectedIndex = NSIndexPath(forRow: indexPath.row, inSection: indexPath.section)
        self.selectedUnitArr[indexPath.section][indexPath.row] = true
        let cell = collectionView.cellForItemAtIndexPath(indexPath) as! CollectionViewCell
        cell.numOfDay.backgroundColor = UIColor.darkGrayColor()
        cell.numOfDay.textColor = UIColor.whiteColor()
        cell.numOfDay.layer.cornerRadius = 15
        cell.numOfDay.clipsToBounds = true
        self.collectView.reloadData()
    }
}

enter image description here

  • 我的猜测:

问题可能是reuse Cell


请教我如何解决这个问题。

非常感谢您的指导和时间。

Ethan Joe

0 个答案:

没有答案