2016-01-09 67 views
1

我有一個集合視圖和一個按鈕在頂部,該按鈕會經歷一個天的列表。對於每一天,收集視圖應該是不同的,我跟蹤字典中的變化。但是,每按一下按鈕,我想重新加載數據,但它選擇隨機單元格。當數據重新加載時,它是否再次運行cellforitematindexpath;如果不是它運行的是什麼?Swift CollectionView ReloadData代理奇怪

這裏是我的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 
    } 

這裏是我的手機有沒有選擇/取消:

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

+0

在您的cellForRowatindex方法文本背景顏色和backView背景顏色都是藍色這就是爲什麼出現此問題 – imjaydeep

+0

我不認爲這是因爲每次我將背景更改爲藍色時,文本立即更改爲白色,反之亦然(cellforrowatindex有一個if功能)。但是我可能不會理解你在說什麼,重新加載數據到底是什麼? – Akbapu

+0

更改文本標籤顏色像紅色測試目的和重新加載你的tableView試試這個 – imjaydeep

回答

1

我相信你應該改變的迅速集合視圖重新加載在左上角的頁面。數據被重新加載以選擇下載列表中最優先的項目。希望這可以幫助!

+0

我不知道你的意思,你能詳細說明我應該改變我的代碼? – Akbapu

+0

非常感謝!有用!!! – Akbapu

+1

解決方案到底是什麼? – Eyzuky