In meiner Hauptansicht Controller habe ich eine segmentierte Steuerung und eine Containeransicht darunter. Ich möchte die Containeransicht ändern, wenn ich das segmentierte Steuerelement umschalte. Wenn ich die Registerkarte umschalte, kann ich die gerade geladene Ansicht sehen (print in viewDidload), aber ich finde es nicht mit der Datenquelle von collectionViewDataSource arbeiten. Irgendwelche Ideen?UICollectionView kann nicht mit segmentierter Steuerung angezeigt werden
// 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
}
}
Wenn ich die PhotoViewController als Anfangs Controller machen, es funktioniert.
Ich denke 'collectionView' wird nicht auf addsubview als eine Ansicht geladen. –
Probieren Sie 'activeVC.collectionView.ReloadData()' in 'updateActiveViewController' Methode aus, bevor Sie die Ansicht der Unteransicht des Containers hinzufügen. –
Es ist nicht Arbeit :( – WeiJay