CollectionView ReloadData Swift问题

时间:2015-04-11 02:13:44

标签: ios core-data calendar

我在应用程序中创建日历,您可以在其中选择您在健身房锻炼的肌肉。 首先,您完成一个表单,数据存储在数据库(CoreData)中。然后我执行查询以检索该数据,以便在我创建的日历中显示它。

事情是日历是一个CollectionView所以我在我找到锻炼的日子里显示了一个色圈,但是当我做一个reloadData时,事情是奇怪的事情...日历显示了几天的圈子是不正确的,但如果我调试代码,我意识到我看到例如3个圆圈,当代码只输入两次创建该圆圈! (我不知道我是否清楚......很难解释它。)

这是我的代码的一部分:

ViewDidLoad方法:

override func viewDidLoad() {
    super.viewDidLoad()
    let date = NSDate()
    let calendar = NSCalendar.currentCalendar()
    let components = calendar.components( .CalendarUnitMonth | .CalendarUnitDay | .CalendarUnitWeekday, fromDate: date)
    let month = components.month

    monthLabel.text = getMonthAtIndex(month-1)
    monthIndex = month-1    // components start at index 1
    fetchMonthData()
    loadCalendayDays()      // for current month
}

创建日历的主要方法:

    func loadCalendayDays() {

        // Setup the calendar object
        let calendar = NSCalendar.currentCalendar()
        let date = NSDate()

        // Create an NSDate for the first and last day of the month
        var components2 = calendar.components(NSCalendarUnit.CalendarUnitYear |
            NSCalendarUnit.CalendarUnitMonth |
            NSCalendarUnit.CalendarUnitWeekday |
            NSCalendarUnit.CalendarUnitDay,
            fromDate: date)

        // Get First and Last date of the month
        components2.month = monthIndex + 1
        components2.day = 1
        let firstDateOfMonth: NSDate = calendar.dateFromComponents(components2)!
        components2.month  += 1
        let lastDateOfMonth: NSDate = calendar.dateFromComponents(components2)!
//      println("first of month: \(firstDateOfMonth)")
//      println("last of month: \(lastDateOfMonth)")

        // get number of days in month
        let unit:NSCalendarUnit = NSCalendarUnit.CalendarUnitDay
        let components3 = calendar.components(unit, fromDate: firstDateOfMonth, toDate: lastDateOfMonth, options: nil)
//      println("number of days in month \(components3.day)")

        // get the day for the first of the month
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
        var firstDayString = dateFormatter.stringFromDate(firstDateOfMonth)

        var weekDay = 0
        if let firstDate = dateFormatter.dateFromString(firstDayString) {
            let myComponents = calendar.components(NSCalendarUnit.CalendarUnitWeekday, fromDate: firstDate)
            weekDay = myComponents.weekday
//          println("first was on a: \(weekDay)")
        }
        else {
//          println("first day is nil")
        }

        var firstWeekDay = weekDay
        var numberOfDaysInMonth = components3.day
        var totalDays = numberOfDaysInMonth + firstWeekDay

        daysArray.removeAll(keepCapacity: true)
        var index = 1
        for var i = 1; i < totalDays; i++ {
            if i < weekDay {
                daysArray.append("0")
            }
            else {
                daysArray.append("\(index++)")
            }
        }

        self.calendarCollView.reloadData()
    }

和cellForItemAtIndexPath方法:

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

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CalendarCell", forIndexPath: indexPath) as! CalendarCell
    var day = daysArray[indexPath.row] as NSString
    if day == "0"
    {
        cell.dayLabel.hidden = true
    }
    else {
        if day == getDay(){
            cell.dayLabel.hidden = false
            cell.dayLabel.text = day as String
            positionToday = indexPath.row
        }
        else{ //Para los demas dias

            cell.dayLabel.hidden = false
            cell.dayLabel.text = day as String
        }
        if day.length == 1 {
            day = "0" + (day as String)
        }
        var aDiasEntrenadosLocal:[String] = recuperoDiasEntrenados()
        while aDiasEntrenadosLocal.count > 0{
            var DiaEntrenado = aDiasEntrenadosLocal[0] as NSString
            if DiaEntrenado as NSString == day{
                if positionToday != nil{
                    cell.dayImage.image = bluePoint
                }
                else{
                    cell.dayImage.image = redPoint
                }
                aDiasEntrenadosLocal.removeAll(keepCapacity: true)
            }
            else{
                aDiasEntrenadosLocal.removeAtIndex(0)
            }
        }//Cierre while
    }
    if indexPath.row == positionToday && formatoMesCalendario() == formatoMesHoy(){ //Otro color al dia de HOY
        //cell.layer.borderColor = UIColor.blackColor().CGColor
        //cell.layer.borderWidth = 3.0
        cell.layer.borderWidth = 1.0
        cell.layer.borderColor = UIColor.lightGrayColor().CGColor
        cell.dayLabel.textColor = UIColor.redColor()
        cell.dayLabel.font = UIFont.boldSystemFontOfSize(17.0)
        cell.dayLabel.text = "-> " + (day as String)
    }
    else
    {//Para los demas dias
        cell.layer.borderWidth = 1.0
        cell.layer.borderColor = UIColor.lightGrayColor().CGColor
    }
    countt++
    return cell
}

感谢您的帮助!

0 个答案:

没有答案