UICollectionViewCell中托管的UITableViewController的滚动问题

时间:2019-05-29 15:47:22

标签: ios swift uikit

我的应用程序中具有以下设置:

UITabBarController
 UINavigationController
  UIViewController

UIViewController具有可水平滚动的UICollectionView。 在单元格中,我想“托管”另一个ViewController的视图。这工作得很好,但是我有滚动问题。第一个UICollectionViewCell承载一个来自UITableViewController的视图。我可以滚动UITableViewController,但是它并不能真正滚动到结尾-似乎UITableViewController开始太早弹跳了。

当我使用UITableViewController作为Root View Controller时,一切工作正常,因此我认为该ViewController没什么问题。

CollectionView的高度很小,我只想显示“弹跳”行为。

这是collectionView的代码:

import Foundation
import UIKit

class FeedSplitViewController : UIViewController, Controllable
{
    @IBOutlet weak var menuBar: MenuBar!
    @IBOutlet weak var collectionView: UICollectionView!

    private var currentIndex = 0
    private var dragStart: CGFloat = 0.0
    private var feedActivities: FeedViewController!

    var controller: Controller!

    override func viewDidLoad()
    {
        super.viewDidLoad()

        self.initControls()
        self.initMenuBar()
        self.initCollectionView()
        self.initActivitiesViewController()
    }

    fileprivate func initActivitiesViewController()
    {
        self.feedActivities = UIStoryboard.instantiate("Main", "feedActivities")
        self.feedActivities.controller = self.controller
    }

    fileprivate func initControls()
    {
        self.navigationController?.navigationBar.setValue(false, forKey: "hidesShadow")
    }

    fileprivate func initMenuBar()
    {
        self.menuBar.showLine = true
        self.menuBar.enlargeIndicator = true
        self.menuBar.texts = [Resources.get("FEED_ACTIVITIES"), Resources.get("DASHBOARD")]

        self.menuBar.selectionChanged =
        {
            index in

            self.collectionView.scrollToItem(at: IndexPath(item: index, section: 0), at: UICollectionView.ScrollPosition.right, animated: true)
        }
    }

    fileprivate func initCollectionView()
    {
        self.collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")

        let menuBarFrame = self.menuBar.frame.origin
        let collectionView = self.collectionView.frame.origin

        Swift.print(menuBarFrame)
        Swift.print(collectionView)
    }
}

extension FeedSplitViewController : UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource
{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        return 2
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)

        if indexPath.item == 0, let feedActivities = self.feedActivities
        {
            cell.contentView.addSubview(feedActivities.view)
        }

        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat
    {
        return 0
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    {
        return CGSize(width: self.view.bounds.width, height: self.view.bounds.height)
    }

    func scrollViewWillBeginDragging(_ scrollView: UIScrollView)
    {
        self.dragStart = scrollView.contentOffset.x
    }

    func scrollViewDidScroll(_ scrollView: UIScrollView)
    {
        let oldIndex = self.currentIndex

        let page = scrollView.contentOffset.x / scrollView.frame.size.width

        let currentPage = Int(round(page))

        if oldIndex != currentPage
        {
            if Settings.useHapticFeedback
            {
                Utilities.haptic(.medium)
            }

            self.menuBar.selectedIndex = currentPage
        }

        self.currentIndex = currentPage
    }
}

我附加了一个小视频:https://imgur.com/a/pj7l3Hd

1 个答案:

答案 0 :(得分:0)

我通过以下操作解决了该问题:

  • 我不再直接在
  • 中托管ViewController的视图
  • 每个UICollectionView单元都托管一个UITableView。
  • UITableViewCell包含先前在ViewController中实现的数据模型。逻辑仍然不在UITableViewCell之外。