2017-04-06 41 views
4

我想了解爲什麼集合視圖保持中心對齊集合中的最後一個單元格。
我已經創建了一個簡單的基於流佈局的集合視圖。我正在使用Autolayout標誌 - 我不確定是否導致此問題。停止單個UICollectionView單元流到屏幕中央

每當我從收集視圖中刪除一個單元格 - 前幾個似乎正常工作,滾到左側。但是,當我刪除倒數第二個時,突然最後一個單元從左對齊變爲中心對齊。 有沒有人有解釋?這似乎很奇怪。

我該如何製作,以便所有單元格都會向左滾動並保持與左側對齊。

編輯:這裏是視圖層次結構的調試: http://imgur.com/a/NxidO

下面是這個視圖行爲

Collectionview centring

我做了一個簡單的github上演示它:https://github.com/grantkemp/CollectionViewIssue

這裏是代碼:

 var dataforCV = ["short","longer phrase", "Super long Phrase"] 

override func viewDidLoad() { 
     super.viewDidLoad() 
     demoCollectionView.reloadData() 
    let layout = UICollectionViewFlowLayout() 

    // Bug Description - > UICollectionViewFlowLayoutAutomaticSize will center Align the layout if it has only a single cell - but it will left align the content if there is more than one cell. 

    // I would expect this behaviour to be consistently left aligning the content. 

    //How to repeat: If you comment out UICollection​View​Flow​Layout​Automatic​Size then the collection view will show 3 cells being left aligned and it will continue to left align all the content no matter how many cells you remove. 

    // But: if you leave turn UICollection​View​Flow​Layout​Automatic​Size on - then it will intially show 3 cells being left aligned, and then as you click to remove each cell they will stay left aligned until the last single cell will suddenly center align in the collection view 

    // see here for the screen recording:https://i.stack.imgur.com/bledY.gif 
    // see here for the view hierachy debuggins screen: http://imgur.com/a/NxidO 

    layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize 


    // <-- End Bug Description 
    demoCollectionView.collectionViewLayout = layout 
} 

    //MARk: CollectionView 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? collectionCell 
     cell?.text.text = dataforCV[indexPath.row] 

     return cell! 

    } 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return dataforCV.count 
    } 
    //Remove the item from the array if selected 
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
     dataforCV.remove(at: indexPath.row) 
     demoCollectionView.deleteItems(at: [indexPath]) 
    } 

回答

0

當最後一個單元格(僅)顯示時,請嘗試查看調試 - >捕獲視圖層次結構(即。假設你在模擬器中運行)。我的預感:一旦你只剩下一個,你的細胞就會變得越來越大

+0

我以爲是這樣 - 但將單元格的背景顏色設爲紅色,並且您可以看到它保持相同的大小 –

+0

不是爲了鞭策死馬,而是您嘗試捕獲視圖層次結構,然後單擊lil圖標以打開顯示主動約束?變成瘋狂的東西有時 – xaphod

+0

謝謝@xaphod - 我不知道視圖調試 - 那很好。我已經完成了對2個單元格和1個單元格的視圖調試 - 看起來有一個新的約束被添加到中心 - ,但我不知道如何解決它。這是你認爲的錯誤嗎? http:// imgur。com/a/NxidO –

3

我設法解決這個問題,使用以下兩個步驟: 1.記錄了一個關於我注意到關於細胞行爲改變時的奇怪行爲的錯誤使用UICollectionViewFlowLayoutAutomaticSize 2.我做了一個通過創建一個修改後的CustomViewFlowLayout(找不到原來的SO/github問題,我看到了這種方法 - 我會添加它,如果我找到它)然後我添加了UICollectionViewFlowLayoutAutomaticSize設置 - 它的工作非常漂亮。

示例代碼:

class MyLeftCustomFlowLayout:UICollectionViewFlowLayout { 
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { 

     let attributes = super.layoutAttributesForElements(in: rect) 

     var leftMargin = sectionInset.left 
     var maxY: CGFloat = 2.0 

     let horizontalSpacing:CGFloat = 5 

     attributes?.forEach { layoutAttribute in 
      if layoutAttribute.frame.origin.y >= maxY 
       || layoutAttribute.frame.origin.x == sectionInset.left { 
       leftMargin = sectionInset.left 
      } 

      if layoutAttribute.frame.origin.x == sectionInset.left { 
       leftMargin = sectionInset.left 
      } 
      else { 
       layoutAttribute.frame.origin.x = leftMargin 
      } 

      leftMargin += layoutAttribute.frame.width + horizontalSpacing 
      maxY = max(layoutAttribute.frame.maxY, maxY) 
     } 

     return attributes 
    } 

在視圖控制器 - 你必須在流佈局添加到集合視圖,使其工作:

let layout = MyLeftCustomFlowLayout() 
layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize 
myCollectionViewReference.collectionViewLayout = layout 

我認爲集合視圖的實現可以肯定簡化一下,但我會更深入地研究它,因爲我可以看到它有多強大。

+0

如果您嘗試支持低於iOS 10.0,則還可以使用'''layout.estimatedItemSize = CGSize(width:1,height:1)'''。 – mn1