菜鸟教程小白 发表于 2022-12-11 16:48:45

ios - uicollectionview 在底部滚动加载更多数据。 iOS swift


                                            <p><p>当我滚动底部到 <code>UICollectionView</code> 时,我想在 <code>UICollectionView</code> 中加载更多数据。我没有找到任何用于 swift 语言的库。谁能告诉我该怎么做?</p>

<p><strong>问题 1</strong> -> 向下滚动时如何加载更多数据</p>

<p><strong>问题 2</strong> -> 如果我能够在滚动到底部时获得更多数据,那么我应该如何添加相同的可变数组。所以它会加载整个数据。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p><strong>Swift 3 和 Swift 4</strong>:CollectionView 委托(delegate)方法</p>

<blockquote>
<p>1) When scroll hit the bottom you can trigger <code>updateNextSet()</code> using <code>willDisplay</code> function</p>
</blockquote>

<pre><code>func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
      if indexPath.row == numberofitem.count - 1 {//numberofitem count
            updateNextSet()
      }
}

func updateNextSet(){
       print(&#34;On Completetion&#34;)
       //requests another set of data (20 more items) from the server.
}
</code></pre>

<blockquote>
<p>2) Append your new data with existing array and reload collection view <code>collectionView.reloadData()</code></p>
</blockquote></p>
                                   
                                                <p style="font-size: 20px;">关于ios - uicollectionview 在底部滚动加载更多数据。 iOSswift ,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/37984327/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/37984327/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - uicollectionview 在底部滚动加载更多数据。 iOS swift