2016-09-23 31 views
2

其他单元格这是我在SWIFT 3UICollectionView斯威夫特卡伦特3显示在部分

class BSViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate { 

    let reuseIdentifier = "cell" // also enter this string as the cell identifier in the storyboard 
    var items = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14","15","16"] 


    // MARK: - UICollectionViewDataSource protocol 

    // tell the collection view how many cells to make 
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 3 
    } 

    // make a cell for each cell index path 
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

     // get a reference to our storyboard cell 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell 

     // Use the outlet in our custom class to get a reference to the UILabel in the cell 
     cell.myLabel.text = self.items[indexPath.item] 
     cell.backgroundColor = UIColor.cyan // make cell more visible in our example project 

     return cell 
    } 

    // MARK: - UICollectionViewDelegate protocol 

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
     // handle tap events 
     print("You selected cell #\(indexPath.item)!") 
    } 

    func numberOfSections(in collectionView: UICollectionView) -> Int { 
     if self.items.count % 3 > 0 { 
      print(self.items.count % 3) 
      return (self.items.count/3)+1 
     } 
     else { 
      return self.items.count/3 
     } 
    } 


    private func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize 
    { 
     var collectionViewSize = collectionView.frame.size 
     collectionViewSize.width = collectionViewSize.width/3.0 //Display Three elements in a row. 
     collectionViewSize.height = collectionViewSize.height/4.0 
     return collectionViewSize 
    } 


    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 
     let view = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "Footer", for: indexPath) 
     // configure footer view 
     return view 
    } 
} 

,结果代码,它显示我6排,每排有3个单元格,它必须是6行,每行包含3个单元格,预计最后一行必须剩下2个单元格。

uncorrect结果

[XXX]

[XXX]

[XXX]

[XXX]

[XXX]

[XXX]

正确的结果

[XXX]

[XXX]

[XXX]

[XXX]

[XXX]

[XX]

+0

因为你是返回'numberOfItemsInSection'为3。 – Santosh

回答

0

这真是一个数学职业布莱姆,斯威夫特没有错。

有两种计算,你必须得到正确的:

  • 有多少部分需要
  • 多少细胞在一个给定的部分

你计算有多少部分,你在需要显示numberOfSections。你的计算是正确的。

你计算出有多少细胞在一个给定的部分numberOfItemsInSection显示。在这里你的计算不正确因为你没有做计算;你总是返回3.因此,每个部分总是包含3个单元格。

要解决这个问题,你需要做的是占了部分区间计算。下面是解决这个的一种方法:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return ((section + 1) * 3) <= self.items.count ? 3 : self.items.count % 3 
} 

顺便说一句,如果你有16个项目,在你的榜样,最后一节应包括细胞。