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

ios - 有没有办法滚动到集合中选定的 UICollectionViewCell ?


                                            <p><p>我的应用程序的模态视图 Controller 中有一个 <code>UICollectionView</code>。当调出模态视图时,集合的一个单元格会根据我从主视图传递到模态视图的某些数据设置为选中状态。</p>

<p>一旦出现带有集合的模态视图,我需要以编程方式将选定的集合单元格滚动到 View 中...但是在填充 Collection View 时使用 <code>scrollToItem(at:at:animated:)</code> (在 <code>cellForItemAt</code> 中)似乎不起作用。</p>

<p>因此,虽然我可以轻松地使用 <code>cellForItemAt</code> 中可用的 <code>indexPath</code> 在填充集合时滚动到选定的单元格,但这是不可能的,我在集合完全填充和呈现后,无法弄清楚如何滚动到当前选定的集合单元格。</p>

<p>我什至不能使用严厉的方法,比如遍历所有集合单元格并检查手动选择的单元格,因为似乎无法遍历当前不可见的单元格。</p>

<p>帮助?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>在顶部添加一个变量。</p>

<pre><code>   private var didLayoutFlag: Bool = false
</code></pre>

<p>只需在 viewDidLayoutSubviews 方法中传递索引号(<code>indexNO</code>)。我正在使用水平滚动。 </p>

<pre><code>   override func viewDidLayoutSubviews() {
            super.viewDidLayoutSubviews()
            if self.colllecitonView != nil{
                if !self.didLayoutFlag {
                  self.colllecitonView.scrollToItem(at:IndexPath(item: indexNO, section:0), at:.right, animated: false)
                  self.didLayoutFlag = true
                }
            }
      }
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 有没有办法滚动到集合中选定的 UICollectionViewCell ?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/44212309/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/44212309/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 有没有办法滚动到集合中选定的 UICollectionViewCell ?