В моем основном представлении Контроллер, у меня есть сегментированный элемент управления и вид контейнера ниже. Я хочу изменить представление контейнера всякий раз, когда я переключаю сегментированный элемент управления. Когда я переключаю вкладку, я вижу, что загружаемое изображение (print in viewDidload), однако, я считаю, что оно не работает с источником данных collectionViewDataSource. Есть идеи?Не удается показать UICollectionView с сегментированным управлением
// Main View
let videoViewController = VideoViewController()
let photoViewController = PhotoViewController()
private var activeViewController: UIViewController? {
didSet {
removeInactiveViewController(oldValue)
updateActiveViewController()
}
}
@IBOutlet weak var containerView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
activeViewController = videoViewController
}
@IBAction func segmentDidChanged(sender: UISegmentedControl) {
if sender.selectedSegmentIndex == 0 {
print("\(sender.titleForSegmentAtIndex(0)!) Selected")
activeViewController = videoViewController
} else if sender.selectedSegmentIndex == 1 {
print("\(sender.titleForSegmentAtIndex(1)!) Selected")
activeViewController = photoViewController
}
}
private func removeInactiveViewController(inactiveViewController: UIViewController?) {
if let inActiveVC = inactiveViewController {
// call before removing child view controller's view from hierarchy
inActiveVC.willMoveToParentViewController(nil)
inActiveVC.view.removeFromSuperview()
// call after removing child view controller's view from hierarchy
inActiveVC.removeFromParentViewController()
}
}
private func updateActiveViewController() {
if let activeVC = activeViewController {
// call before adding child view controller's view as subview
addChildViewController(activeVC)
activeVC.view.frame = containerView.bounds
containerView.addSubview(activeVC.view)
// call before adding child view controller's view as subview
activeVC.didMoveToParentViewController(self)
}
}
// PhotoView
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
print("HI")
}
extension PhotoViewController : UICollectionViewDataSource {
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 2
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 2
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as!PhotoCollectionViewCell
cell.backgroundColor = UIColor.blackColor()
// Configure the cell
cell.imgView.image = UIImage(named: "test")
return cell
}
}
Когда я делаю PhotoViewController в качестве исходного контроллера, он работает.
Я думаю, что 'collectionView' не загружается при добавлении в виде представления. –
попробуйте 'activeVC.collectionView.reloadData()' в 'updateActiveViewController' перед добавлением своего представления в subview контейнера. –
Это не работает :( – WeiJay