2015-07-10 172 views
1

我有兩個nib文件分別是RowRecordLayout和ColumnLayout。 enter image description here enter image description here佈局在UICollectionViewCel看起來很奇怪,當執行setCollectionViewLayout

我做登記筆尖細胞UICollectionView被用於在cellForItemAtIndexPath功能以後使用。這裏是我的代碼

override func viewDidLoad() { 
self.DJCollectionView.registerNib(UINib(nibName: "RowTemplateCell", bundle: NSBundle.mainBundle()), forCellWithReuseIdentifier: "DJCELL") 
     self.DJCollectionView.registerNib(UINib(nibName: "ColumnTemplateCell", bundle: NSBundle.mainBundle()), forCellWithReuseIdentifier: "DJCELL2") 
} 
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath 
    indexPath: NSIndexPath) -> UICollectionViewCell { 

    var ProductCardCellpt : DJCell? 

    if self._CurrentLayoutType == enum_Layout.RowRecords { 
    reuseIdentifier = "DJCELL" 
    ProductCardCellpt = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as? DJCell 
    … 
    }else{ 
    reuseIdentifier = "DJCELL2" 
    ProductCardCellpt = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as? DJCell 
    ... 
    } 
    return ProductCardCellpt! 
    } 

最後當我觸摸DJ按鈕。轉變之間的動畫是確定除了所述細胞

Initial LayoutNew Layout

這裏內部的意外約束是過渡佈局的代碼

@IBAction func Action_ToggleLayout(sender: UIBarButtonItem) { 
     if _CurrentLayoutType == enum_Layout.TwoColumn { 
      _CurrentLayoutType = enum_Layout.RowRecords 
      self.DJCollectionView.setCollectionViewLayout(self._RowRecordsLayout_CollectionViewLayout!, animated: true, completion: { (IsDone) -> Void in 

      }) 
     } 
     else{ 
      _CurrentLayoutType = enum_Layout.TwoColumn 
      self.DJCollectionView.setCollectionViewLayout(self._TwoColumnLayout_CollectionViewLayout!, animated: true, completion: { (IsDone) -> Void in 

       } 
      }) 
     } 

    } 

在細胞內的內容均恢復正常時,我向上滾動或下。 任何解決方案來解決這個問題?

+1

你可以把的CollectionView佈局在筆尖文件?你只能把UICollectionViewCell放入筆尖,不是嗎? –

+0

它們是筆尖中的UICollectionViewCell。 – Deathz

+0

你什麼時候改變了collectionView的佈局? –

回答

0

之後我試了很多東西。我結合了Poql的答案來實現結果。這是程序。

  1. 不要寄存器單元類UICollectionView沒有筆尖連接到它 在自定義單元格類使用initwithframe因爲沒有筆尖文件,並沒有故事板指細胞
  2. 申報功能細胞的類中,用於切換佈局實現
  3. 細胞的類內
  4. 覆蓋功能willTransitionFromLayout
  5. 細胞的類內覆蓋功能applyLayoutAttributes
  6. 最後,Callling collectionView.dequeueReusableC後翻轉功能ellWithReuseIdentifier中的CollectionView(的CollectionView:UICollectionView,cellForItemAtIndexPath indexPath:NSIndexPath)功能

下面是代碼的一部分享受

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    var ProductCardCellpt : ProductCardCell? = collectionView.dequeueReusableCellWithReuseIdentifier("ProductCardCell", forIndexPath: indexPath) as? ProductCardCell 
    ProductCardCellpt?.SetLayoutAccordingToDesiredLayout(self._CurrentLayoutType) 
} 

細胞的類中

func SetLayoutAccordingToDesiredLayout(DesiredLayout : enum_Layout){ 
    if DesiredLayout == self.Current_Layout && IsSetPosition == true { 
     return 
    } 
    self.IsSetPosition = false 
    self.Current_Layout = DesiredLayout 
    if DesiredLayout == enum_Layout.OneColumn || DesiredLayout == enum_Layout.TwoColumn { 
    DoColumnLayout() 
    }else{ 
    DoRecordLayout() 
    } 
} 
func DoRecordLayout(){ 
    let dFrame = self.contentView.frame 
    self.Thumbnail.frame = CGRect(x: 0, y: 0, width: dFrame.height, height: dFrame.height) 
    ... 
} 
func DoColumnLayout(){ 
    let dFrame = self.contentView.frame 
    self.Thumbnail.frame = CGRect(x: 0, y: 0, width: dFrame.width, height: dFrame.width) 
    ... 
} 
override func applyLayoutAttributes(layoutAttributes: UICollectionViewLayoutAttributes!) { 
    self.layoutIfNeeded() 
    self.SetLayoutAccordingToDesiredLayout(self.Current_Layout) 

} 
override func willTransitionFromLayout(oldLayout: UICollectionViewLayout, toLayout newLayout: UICollectionViewLayout) { 
    let nLayout = newLayout as! ProductCollectionViewLayout 
    let enumLayout = nLayout._enumLayout 
    self.Current_Layout = enumLayout 
} 
0

那麼問題是當您更改佈局時,當前顯示的單元格不會更新。它們只在滾動時更新。在佈局改變時做類似調用collectionView.reloadData()的工作應該可以完成這項工作。

但我不認爲你正確使用collectionView。佈局與UICollectionViewCell無關,動畫將無法使用。所以我不認爲在更改UICollectionView的佈局時更改collectionView單元類是不好的做法。您應該重寫自定義collectionViewCell的方法willTransitionFromLayout:並在此處更改單元格的約束。像Working with AutoLayout in Portrait and Landscape

希望這可以引導你正確的方式。