I'm new to Xcode so apologies if I'm missing something obvious but I've been stuck on this for days now.
I've created a UICollectionView - collection view in storyboard
There are constraints in place 0 to each side of the view, and also one to the label above. The image and the label in the cell also have constraints.
when I run the application I get this - Running application
I'm not sure how that is possible given the constraints. But If I remove any constraint, edit the existing ones, or remove them entirely I'm just getting a SIGABRT exception.
I'm extremely confused, if my code is compiling and running, I'm assuming that the code populating the UICollectionView is working. Is there something
TestVC.swift
import UIKit
class TestVC: UIViewController, UICollectionViewDelegate,UICollectionViewDataSource {
@IBOutlet weak var myCollection: UICollectionView!
// making this an empty array incase the data hasn't loaded yet.
private(set) public var testArrays = [dataArrays]()
override func viewDidLoad() {
super.viewDidLoad()
myCollection.dataSource = self
myCollection.delegate = self
}
func initData(category: Category){
testArrays = DataService.instance.testData(forCategoryTitle: category.title)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return testArrays.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath) as? collectionViewCell{
let testArray = testArrays[indexPath.row]
cell.updateViews(testArray: testArray)
return cell
}
return collectionViewCell()
}
}
collectionViewCell.swift
import UIKit
class collectionViewCell: UICollectionViewCell {
@IBOutlet weak var cellImage: UIImageView!
@IBOutlet weak var cellLabel: UILabel!
func updateViews(dataarray: dataArrays) {
cellImage.image = UIImage(named: dataarray.imageName)
cellLabel.text = dataarray.title
}
}
The Cell itself wasn't registered in the Attributes Inspector, I changed it to - collectionViewCell and now it's working.
I also has an issue with constraints, I wasn't reading the Output terminal entirely, being new to this it all looked a bit cryptic but I was able to troubleshoot through the output.
This was very helpful https://developer.apple.com/videos/play/wwdc2015/219/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.