Posts

Post not yet marked as solved
8 Replies
Hey thank you Claude31 and OOper!Your advice/suggestions pointed me in the right direction.Turns out the weird behavior was not due to a single underlying bug, but a combination of multiple smaller bugs.Everything is fixed and working now. Thanks!
Post not yet marked as solved
8 Replies
I tried to access collectionView.parent from within CollectionView.swift (which doesn't work). Any ideas why this isn't working?Yes, I've updates the delegates. They are now:self.delegate = viewController self.dataSource = viewControllerUnfortunately, even with your suggestions implemented, the behavior is unchanged. Any further thoughts?Here is my updates code:(FlowLayout.swift is the same as before)ViewController.swiftimport UIKit class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { var gridCollectionView: GridCollectionView! override func viewDidLoad() { super.viewDidLoad() gridCollectionView = newCollectionView() } func newCollectionView() -> GridCollectionView { let gridCollectionView = GridCollectionView(viewController: self) self.view.addSubview(gridCollectionView) gridCollectionView.activateConstraints() return gridCollectionView } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 64 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath as IndexPath) as! CollectionViewCell cell.label.text = "\(indexPath.item)" cell.backgroundColor = UIColor.gray // Comment this line out and the 8x8 grid turns into a 4x4 grid! return cell } }CollectionView.swiftimport UIKit class GridCollectionView: UICollectionView { var viewController: ViewController! init(viewController: ViewController) { self.viewController = viewController let width = viewController.view.frame.width - 20 let frame = CGRect(x: 0, y: (viewController.view.frame.height - width)/2, width: width, height: width) let gridLayout = GridFlowLayout() super.init(frame: frame, collectionViewLayout: gridLayout) self.delegate = viewController self.dataSource = viewController self.register(CollectionViewCell.self, forCellWithReuseIdentifier: "Cell") self.backgroundColor = UIColor.red } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } func activateConstraints() { let view = viewController.view! translatesAutoresizingMaskIntoConstraints = false centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true NSLayoutConstraint(item: self, attribute: NSLayoutConstraint.Attribute.centerY, relatedBy: NSLayoutConstraint.Relation.equal, toItem: view, attribute: NSLayoutConstraint.Attribute.centerY, multiplier: 1, constant: -50).isActive = true widthAnchor.constraint(equalToConstant: view.bounds.width).isActive = true heightAnchor.constraint(equalToConstant: view.bounds.width).isActive = true contentInsetAdjustmentBehavior = .always } }Again, any help is greately appreciated!
Post not yet marked as solved
8 Replies
"Why do store a reference to its ViewController ? If you need to access it, just call clllectionView.parent."Negative. That doesn't work as I don't have access to "collectionView".When I try to call collectionView.parent, I get the following error message:Use of unresolved identifier 'collectionView';
Post not yet marked as solved
8 Replies
Hi Ooper.I've boiled my project down to only its most salient components.Below is a link to my project. This way, you can see you see my .xib in action in addtion to my source code.workupload [DoT} c0m SlAsH file sLaSh jKSaNzJJAs you can see, when you try to run my code, you get an[ViewController collectionView:numberOfItemsInSection]: unrecognized selector sent to instance 0x7fcadfc0d690error.I am not sure what this means or what is causing this.Any help is much appreciated!
Post not yet marked as solved
8 Replies
Call me a noob if you have to (which am), but if I addweak var parent: UIViewController? { get }to my code, I get a build-time error "Expected '{' to start getter definition"But if I delete the above line, I then get the error 'Use of unresolved identifier 'parent'Clearly, I must be doing something wrong?I did explicitly added the child view in the parent view controller by callingself.view.addSubview(mySubView)
Post not yet marked as solved
4 Replies
Just some simple properties.For now, all I need is a lable that will store a String, some representation to store a background color, and a boolean to store a state (in order to display the correct background color).I might expand its functionality later, but the above is all I need at this point.