菜鸟教程小白 发表于 2022-12-11 17:38:15

ios - Swift 3 中的 UICollectionView 在部分中显示额外的单元格


                                            <p><p>这是我在 swift 3 中的代码</p>

<pre><code>    class BSViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

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


    // MARK: - UICollectionViewDataSource protocol

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

    // make a cell for each cell index path
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; 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
      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(&#34;You selected cell #\(indexPath.item)!&#34;)
    }

    func numberOfSections(in collectionView: UICollectionView) -&gt; Int {
      if self.items.count % 3 &gt; 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) -&gt; 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) -&gt; UICollectionReusableView {
      let view = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionFooter, withReuseIdentifier: &#34;Footer&#34;, for: indexPath)
      // configure footer view
      return view
    }
}
</code></pre>

<p>结果,它显示了 6 行,每行有 3 个单元格,并且必须是 6 行,每行包含 3 个单元格,期望最后一行必须有 2 个剩余单元格。</p>

<p>结果不正确</p>

<p>[ X X X ]</p>

<p>[ X X X ]</p>

<p>[ X X X ]</p>

<p>[ X X X ]</p>

<p>[ X X X ]</p>

<p>[ X X X ]</p>

<p>正确的结果</p>

<p>[ X X X ]</p>

<p>[ X X X ]</p>

<p>[ X X X ]</p>

<p>[ X X X ]</p>

<p>[ X X X ]</p>

<p> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>这真的是一道数学题,Swift 没有错。</p>

<p>有两个计算你必须做对:</p>

<ul>
<li>您需要多少个部分</li>
<li>在给定部分中显示多少个单元格</li>
</ul>

<p>您可以在 <code>numberOfSections</code> 中计算需要多少个部分。你的计算是正确的。</p>

<p>您可以计算在 <code>numberOfItemsInSection</code> 的给定部分中显示多少单元格。您在此处的计算是<strong>不正确</strong>,因为您没有进行计算;你总是返回 3。因此每个部分总是包含 3 个单元格。</p>

<p>要解决此问题,您需要进行计算部分部分的计算。以下是解决此问题的一种方法:</p>

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

<p>顺便说一句,如果您有 16 个项目,如您的示例所示,最后一部分应包含 <strong>1</strong> 单元格。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - Swift 3 中的 UICollectionView 在部分中显示额外的单元格,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/39668339/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/39668339/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - Swift 3 中的 UICollectionView 在部分中显示额外的单元格