Swift CollectionView ReloadData表现奇怪

时间:2016-01-09 01:00:01

标签: ios swift uicollectionview

我有一个Collection View和一个顶部的按钮,该按钮会查看天数列表。对于每一天,集合视图应该是不同的,我跟踪字典中的更改。但是,每次按下按钮,我都想重新加载数据,但是它会选择随机单元格。重新加载数据时,它是否再次运行cellforitematindexpath;如果不是它究竟运行了什么?

这是我的CellForItem代码:

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("availability", forIndexPath: indexPath) as! AvailabilityCell
        var tempList: [Int]
        tempList = self.availability[self.days[index]]! as! [Int]
        cell.timeInterval.text = timeIntervals[indexPath.row]
        cell.timePeriod.text = timesForIntervals[indexPath.row]
        cell.timeInterval.textColor = DARK_BLUE
        cell.timePeriod.textColor = DARK_BLUE
        cell.backView.layer.cornerRadius = 10
        cell.backView.layer.masksToBounds = true
        cell.backView.layer.borderColor = DARK_BLUE.CGColor
        cell.backView.layer.borderWidth = 1
        cell.selected = false
    if (tempList[indexPath.row] == 1) {

                cell.timeInterval.textColor = UIColor.whiteColor()
                cell.backView.backgroundColor = DARK_BLUE
                cell.timePeriod.textColor = UIColor.whiteColor()

    }
        return cell
    }

这是我的Cell选择/取消选择:

 func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

        let cell = collectionView.cellForItemAtIndexPath(indexPath) as! AvailabilityCell
        cell.timeInterval.textColor = UIColor.whiteColor()
        cell.backView.backgroundColor = DARK_BLUE
        cell.timePeriod.textColor = UIColor.whiteColor()
        var tempList: [Int]
        tempList = self.availability[self.days[index]]! as! [Int]
        tempList[indexPath.row] = 1
        self.availability[self.days[index]] = tempList

    }

    func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
        let cell = collectionView.cellForItemAtIndexPath(indexPath) as! AvailabilityCell
        cell.timeInterval.textColor = DARK_BLUE
        cell.backView.backgroundColor = UIColor.whiteColor()
        cell.timePeriod.textColor = DARK_BLUE
        var tempList: [Int]
        tempList = self.availability[self.days[index]]! as! [Int]
        tempList[indexPath.row] = 0
        self.availability[self.days[index]] = tempList

    }

这是重新加载数据的地方:

@IBAction func decreaseDay(sender: AnyObject) {
        index--
        day.text = days[index]
        dispatch_async(dispatch_get_main_queue()) {
            self.timeCollection.reloadData()
        }
        if (index == 0) {
            back.enabled = false
        }

        forward.enabled = true
    }

以下是对正在发生的事情的看法: 它如何开始: How it starts

当我重新加载时: What Happens when I reload

1 个答案:

答案 0 :(得分:1)

我相信您应该更改swift集合视图以重新加载左上角的页面。重新加载数据以选择下载列表中的最优先项。希望这可以帮助!