菜鸟教程小白 发表于 2022-12-13 16:11:25

ios - 当我滚动 UITableview 时自动滚动 UICollectionView


                                            <p><p>我在上面有一个水平的 <code>CollectionView</code> 和它下面的 <code>TableView</code>
我希望在滚动 <code>TableView</code> 时,我的 <code>CollectionView</code> 应该滚动和动画到我用 <code>scrollItemTo</code> 但 <code>CollectionView</code> 滚动到慢,但我希望它在 <strong>uberEats</strong> iOS 应用程序中的餐厅项目列表详细信息中工作,并且在 <strong>urbanClap</strong> 应用程序中工作</p>

<p><em>就像表格 View 部分标题到达顶部时逐个单元格移动 View 单元格一样</em></p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您所指的 uber Eats 应用功能的工作方式如下:每当 <code>tableView</code> 的特定部分到达顶部时,就会选择该特定的 <code>collectionViewCell</code>。 </p>

<p>从上面的陈述可以看出,</p>

<p><strong><code>collectionView</code> 中的项目数 = <code>tableView</code></strong></p> 中的部分数

<p>您可以通过跟踪 <code>tableView</code> 的顶部可见部分索引,然后在该特定索引处选择 <code>collectionViewCell</code> 来解决此特定问题,即</p>

<pre><code>func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView === self.tableView {
      if
            let topSectionIndex = self.tableView.indexPathsForVisibleRows?.map({ $0.section }).sorted().first,
            let selectedCollectionIndex = self.collectionView.indexPathsForSelectedItems?.first?.row,
            selectedCollectionIndex != topSectionIndex {
            let indexPath = IndexPath(item: topSectionIndex, section: 0)
            self.collectionView.selectItem(at: indexPath, animated: true, scrollPosition: .centeredHorizontally)
      }
    }
}
</code></pre>

<p> <a href="/image/wDM2u.gif" rel="noreferrer noopener nofollow"><img src="/image/wDM2u.gif" alt="enter image description here"/></a> </p>

<p><strong>在选择时更改 <code>collectionViewCell</code> 颜色:</strong></p>

<p>创建一个自定义 <code>UICollectionViewCell</code> 并覆盖 <code>**isSelected**</code> 属性以处理选中和未选中状态,即</p>

<pre><code>class CollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var titleLabel: UILabel!

    override var isSelected: Bool {
      didSet {
            if self.isSelected {
                self.contentView.backgroundColor = #colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1)
            } else {
                self.contentView.backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
            }
      }
    }
}
</code></pre>

<p>此后,您无需在其他地方手动更新单元格的 <code>backgroundColor</code>。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 当我滚动 UITableview 时自动滚动 UICollectionView,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/56072349/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/56072349/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 当我滚动 UITableview 时自动滚动 UICollectionView