【问题标题】:Auto scroll UICollectionView when I scroll UITableview当我滚动 UITableview 时自动滚动 UICollectionView
【发布时间】:2019-09-28 01:16:28
【问题描述】:

我在顶部有一个水平的CollectionView,在它下面有一个TableView 当我滚动TableView 时,我希望我的CollectionView 应该滚动和动画到我用scrollItemTo 实现的某个级别,但CollectionView 滚动速度很慢,但我希望它在uberEats 餐厅项目列表详细信息中的 iOS 应用程序和 urbanClap 应用程序中的 iOS 应用程序

这就像在表格视图部分标题到达顶部时逐个单元格移动视图单元格

【问题讨论】:

  • 将collectionview添加到tableview header或者添加到第一个table view cell
  • 添加你目前使用的代码。

标签: ios uitableview uicollectionview scrollview swift4.2


【解决方案1】:

您可以为您的 tableView 实现 scrollViewDidScroll 或 UIScrollViewDelegate,然后从那里手动滚动您的 UICollectionView

class XYZ: UIViewController, UIScrollViewDelegate{

     func viewDidLoad(){
         super.viewDidLoad()
         tableView.delegate = self
     }


    func scrollViewDidScroll(_ scrollView: UIScrollView){
         let tableView = scrollView //assuming the only scrollView.delegate you conform to is the tableVeiw

         collectionView.contentOffset = someMappingFrom(tableView.contentOffset) //or some other scrolling mechanism (scrollToItem)
    }


}

【讨论】:

    【解决方案2】:

    您所指的 uber Eats 应用程序功能的工作方式如下:每当 tableView 的特定部分到达顶部时,就会选择特定的 collectionViewCell。

    从上面的陈述可以看出,

    collectionView 中的项目数 = tableView 中的部分数

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

    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)
            }
        }
    }
    

    在选择时更改 collectionViewCell 颜色:

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

    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)
                }
            }
        }
    }
    

    此后您无需手动更新单元格的backgroundColor。

    【讨论】:

    • 我的单元格颜色在我向下滚动时不会改变,只有在我向上滚动时才会改变
    • 你是如何实现细胞色彩变化逻辑的?
    • func changeCellColor(indexPath: IndexPath){ if let collectIndex = self.collectionView.cellForItem(at: indexPath){ let cell = collectIndex as! CollectionViewCell UIView.animate(withDuration: 1) { cell.containerView.layer.backgroundColor = UIColor.black.cgColor cell.cellLabel.textColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1) } } }
    • 我在这里称之为 self.collectionView.selectItem(at: indexPath, animated: true, scrollPosition: .left) changeCellColor(indexPath: indexPath)
    • 我做到了兄弟,我希望我能按 1000 次
    【解决方案3】:

    您需要更改 scrollViewDidScroll 上的选择。

    我已附上相同代码的 repo 链接

    Github Repo for solution

    【讨论】:

      猜你喜欢
      • 2018-11-02
      • 1970-01-01
      • 2013-02-05
      • 1970-01-01
      • 2016-06-04
      • 1970-01-01
      • 2018-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多