簡體   English   中英

Swift 3中的UICollectionView在各節中顯示其他單元格

[英]UICollectionView in Swift 3 display additional Cells in sections

這是我在Swift 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個剩余單元格。

結果不正確

[XXX]

[XXX]

[XXX]

[XXX]

[XXX]

[XXX]

正確的結果

[XXX]

[XXX]

[XXX]

[XXX]

[XXX]

[XX]

這確實是一個數學問題,Swift沒有任何問題。

您必須正確進行兩個計算:

  • 您需要多少節
  • 給定部分中要顯示多少個單元格

您可以在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個項目(例如您的示例),則最后一節應包含1個單元格。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM