美文网首页
Swift-联动(tableView加CollectionVie

Swift-联动(tableView加CollectionVie

作者: SK丿希望 | 来源:发表于2019-08-14 10:05 被阅读0次

有头部悬浮

使用条件:
必须有头部悬浮View(因为是根据头部的显示或消失来确认角标)

  • 核心代码(左边)
    // 左边单元格选中时调用
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.scrollToRow(at: indexPath, at: UITableView.ScrollPosition.top, animated: true) // 移动左边
        self.collectionView.scrollToItem(at: IndexPath(item: 0, section: indexPath.row), at: UICollectionView.ScrollPosition.top, animated: true) // 滑动右边
    }
  • 核心代码(右边)
    func collectionView(_ collectionView: UICollectionView, willDisplaySupplementaryView view: UICollectionReusableView, forElementKind elementKind: String, at indexPath: IndexPath) {
        if !(collectionView.isDragging || collectionView.isDecelerating) {
            return
        }
        if rightTableIsScrollDown {
            HWPrint("上滑")
        } else {
            HWPrint("下滑")
            leftTableView.selectRow(at: IndexPath(row: indexPath.section, section: 0), animated: true, scrollPosition: .top)
        }
    }
    func collectionView(_ collectionView: UICollectionView, didEndDisplayingSupplementaryView view: UICollectionReusableView, forElementOfKind elementKind: String, at indexPath: IndexPath) {
        if !(collectionView.isDragging || collectionView.isDecelerating) {
            return
        }
        if rightTableIsScrollDown {
            HWPrint("上滑")
            leftTableView.selectRow(at: IndexPath(row: indexPath.section+1 , section: 0), animated: true, scrollPosition: .top)
        } else {
            HWPrint("下滑")
        }
    }
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView == leftTableView { return } // 如果是左边就结束
        rightTableIsScrollDown = rightTableLastOffsetY < scrollView.contentOffset.y // 记录是上滑还是下滑
        rightTableLastOffsetY = scrollView.contentOffset.y // 记录当前位置
    }

Dome

无头部悬浮

优点:不需要头部悬浮也可以确认角标
缺点:点击左边时,必须将右边的滚动动画关闭, 不然会触发多次scrollViewDidScroll引起不必要的bug

  • 核心代码(左边)
    //单元格选中时调用
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.selectRow(at: indexPath, animated: true, scrollPosition: UITableView.ScrollPosition.top)
        self.collectionView.scrollToItem(at: IndexPath(item: 0, section: indexPath.row), at: UICollectionView.ScrollPosition.top, animated: false) //  animated 如果为true 上面方便必须注释 不然 scrollViewDidScroll(_ scrollView: UIScrollView) 会多次调用
    }
  • 核心代码(右边)
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView == leftTableView { return } // 如果是左边就结束
        if let indexPath = collectionView.indexPathForItem(at: CGPoint(x: 0, y: scrollView.contentOffset.y))  {
            let moveToIndexPath = IndexPath(item: indexPath.section, section: 0)
            if leftTableView.indexPathForSelectedRow != moveToIndexPath { // 判断选择的是否是当前的
                DispatchQueue.main.async { [weak self] in
                    self?.leftTableView.selectRow(at: moveToIndexPath, animated: true, scrollPosition: UITableView.ScrollPosition.top)
                }
            }
        }
    }

Dome

可能会出现某些问题(将其加入主线程animated: 设置false)

        DispatchQueue.main.async {[weak self] in
            self?.leftTableView.selectRow(at: IndexPath(row: indexPath.section, section: 0), animated: false, scrollPosition: .top)
        }

相关文章

网友评论

      本文标题:Swift-联动(tableView加CollectionVie

      本文链接:https://www.haomeiwen.com/subject/bvmwjctx.html