Проблема прокрутки с UITableViewController, размещенным в UICollectionViewCell - PullRequest
1 голос
/ 29 мая 2019

В моем приложении есть следующие настройки:

UITabBarController
 UINavigationController
  UIViewController

У UIViewController есть UICollectionView с горизонтальной прокруткой.В ячейках я хочу «разместить» представление из другого ViewController.Это работает довольно хорошо, но у меня есть проблемы с прокруткой.Первый UICollectionViewCell содержит представление, полученное из UITableViewController.Я могу прокрутить UITableViewController, но он на самом деле не прокручивается до конца - кажется, что UITableViewController начинает слишком рано отскакивать.

Когда я использовал UITableViewController в качестве контроллера корневого просмотра, все работало нормально, поэтому яне думайте, что с этим 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 голосов
/ 06 июня 2019

Я решил это, выполнив следующее:

  • Я больше не размещаю представление ViewController непосредственно в
  • Каждая ячейка UICollectionView содержит UITableView.
  • UITableViewCell содержит модель данных, которая ранее была реализована в ViewController. Логика все еще находится за пределами UITableViewCell.
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...