UICollectionView first reusable table cell not showing up - swift

I am working on a game right now and my level selection isn't working right.
The first cell in the collection view won't show up and I can't figure out why.
Here is what it looks like:
Picture of collection view with missing cell
And here is my code for the UICollectionViewController:
the reuse IdentifierList is a list of strings that corresponds to the cells in the storyboard version of this. and I have already checked that they are all spelled correctly.
The one missing is the "MyCell" cell.
class UICollectionCollectionViewController: UICollectionViewController {
var reuseIdentifierList = ["MyCell",
"MyCell2",
"MyCell3",
"MyCell4",
"MyCell5",
"MyCell6",
"MyCell7",
"MyCell8",
"MyCell9",
"MyCell10",
"MyCell11",
"MyCell12",
"MyCell13",
"MyCell14",
"MyCell15",
"MyCell16",
"MyCell17",
"MyCell18"
]
override func viewDidLoad() {
super.viewDidLoad()
reuseIdentifier = reuseIdentifierList[0]
self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: UICollectionViewDataSource
override func numberOfSections(in collectionView: UICollectionView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return reuseIdentifierList.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierList[indexPath.item], for: indexPath)
if indexPath.item != indexPath.startIndex {
cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierList[indexPath.item], for: indexPath)
}else{
cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierList[0], for: indexPath)
}
print(indexPath.item)
print(reuseIdentifierList[indexPath.item])
// Configure the cell
return cell
}
#IBAction func unwindToViewControllerLevel (sender: UIStoryboardSegue){
}
}
Thanks in advance

There is no point in creating that many cells. Just have an array to serve as your data source. In your case, it looks like you want your cells to have a button with numbers in order.
class UICollectionCollectionViewController: UICollectionViewController {
// let dataSourceArray = [Int](1...20) // [1, 2, 3 ..., 20]
// EDIT -- SegueId
let dataSourceArray = [(name: "1", segueId: "segueId1"), (name: "2", segueId: "segueId2"), (name: "3", segueId: "segueId3")]
let cellId = "customCellId"
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView?.register(CustomCell.self, forCellWithReuseIdentifier: cellId)
}
// MARK: UICollectionViewDataSource
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return dataSourceArray.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
let item = dataSourceArray[indexPath.item]
cell.button.setTitle(item.name, for: .normal)
let segueId = item.segueId
// use segue id to perform different segues
return cell
}

Related

How to Navigate to new View Controller from collection view cell? In Swift

I have two View controllers(VC-1, VC-2).
View controller 1 is having a table view and further that table view is consists of 4 collection views. I want to navigate to new VC(VC-2). But unable to get "self.storyboard.instantiateViewController" in didSelect method of collection View.
So now how I can navigate to VC-2
You should use delegate. you can create a protocol and define a method for routing and implement it into VC1
You can use the below sample code.
This code is using closure syntax
class FirstVC: UIViewController, UITableViewDataSource, UITableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableCell_Identifier") as! TableCell
cell.configureCell(data: ["Your", "Data"])
cell.didSelectRow = { data in
// Goto second view controller
let vc = self.storyboard?.instantiateViewController(withIdentifier: "SecondVC")
self.navigationController?.pushViewController(vc!, animated: true)
}
return cell
}
}
class TableCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate {
#IBOutlet weak var colView: UICollectionView!
var didSelectRow: ((_ data: String) -> Void)? = nil // Closure
var arrData = [String]()
func configureCell(data: [String]) {
// Setup CollectionView data
self.arrData = data
self.colView.reloadData()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.arrData.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CELL_IDENTIFIER", for: indexPath)
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let data = self.arrData[indexPath.row]
didSelectRow?(data)
}
}

Cells are not populating in second collection view inside of table

I am new to coding and I am losing my mind with my current problem. I have a table view controller with 7 cells. I have 4 collection views in 7 of the cells. I have labels in the other three. I am trying to get my first two collection views to work so I can attack the same approach to add the other two. I am currently only able to get the cells of my first collection view (in cell 2) two populate. I don't know how to get the cells of the second collection view to populate.
Here is the code for the table view controller:
import UIKit
class HomeTableViewController: UITableViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
#IBOutlet weak var t10MacInfCollectionView: UICollectionView!
#IBOutlet weak var t10MicroInfCollectionView: UICollectionView!
let T10Mac = "T10Mac"
let T10Mic = "T10Mic"
override func viewDidLoad() {
super.viewDidLoad()
t10MacInfCollectionView.delegate = self
t10MicroInfCollectionView.delegate = self
t10MacInfCollectionView.dataSource = self
t10MicroInfCollectionView.dataSource = self
self.view.addSubview(t10MicroInfCollectionView)
self.view.addSubview(t10MicroInfCollectionView)
t10MacInfCollectionView.reloadData()
t10MicroInfCollectionView.reloadData()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == self.t10MacInfCollectionView {
return 10
}
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == self.t10MacInfCollectionView {
let cellA = collectionView.dequeueReusableCell(withReuseIdentifier: T10Mac , for: indexPath) as! T10MacroCollectionViewCell
return cellA
}
else {
let cellB = collectionView.dequeueReusableCell(withReuseIdentifier: T10Mic , for: indexPath) as! T10MicroCollectionViewCell
return cellB
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if collectionView == self.t10MacInfCollectionView {
return CGSize(width: 125.0, height: 225.0)
}
else {
return CGSize(width: 125.0, height: 225.0)
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 7
}
}
I am literally so frustrated. If someone could please help, I will be so thankful. Thank you guys for the help.
In the viewDidLoad() func: delete
self.view.addSubview(t10MicroInfCollectionView)
self.view.addSubview(t10MicroInfCollectionView)
The viewDid should now look like:
override func viewDidLoad() {
super.viewDidLoad()
t10MacInfCollectionView.delegate = self
t10MicroInfCollectionView.delegate = self
t10MacInfCollectionView.dataSource = self
t10MicroInfCollectionView.dataSource = self
t10MacInfCollectionView.reloadData()
t10MicroInfCollectionView.reloadData()
}

UICollectionViewDelegate method not called in UICollectionViewController subclass

Delegate and datasource are also setI have subclassed the UICollectionViewController, all datasource method of UICollectionView are getting called but UICollectionViewDelegate method are not getting called. What could be the reason. ? Below is the implementaion of UICollectionViewController subclass.
class CollectionCollectionViewController: UICollectionViewController {
let carImages = ["mini_small","rover_small","smart_small","highlander_small","venza_small","volvo_small","vw_small","nissan_small","honda_small","jeep_small"]
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
// MARK: UICollectionViewDataSource
override func numberOfSections(in collectionView: UICollectionView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return carImages.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath) as! MyCollectionViewCell
let path = Bundle.main.url(forResource: carImages[indexPath.row], withExtension: "jpg")
let data = try! Data(contentsOf: path!)
cell.imgView.image = UIImage(data: data)
return cell
}
// MARK: UICollectionViewDelegate
// Uncomment this method to specify if the specified item should be highlighted during tracking
override func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {
return true
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let path = Bundle.main.url(forResource: carImages[indexPath.row], withExtension: "jpg")
let data = try! Data(contentsOf: path!)
let image = UIImage(data: data)
return image!.size
}
try this
self.collectionView.delegate = self
Hope this help

Why does my UICollectionViewController not show anything?

I'm trying to make a UICollectionViewController show a grid of cells (in which I'll later add text, likely as textViews if possible). I have:
class GridViewController : UICollectionViewController{
//(NOW GONE) #IBOutlet weak var gridViewTable = UICollectionViewController()
let arr1 : [String] = []
let arr2 : [String] = []
override func viewDidLoad(){
super.viewDidLoad()
self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "gridCell")
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 4
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath as IndexPath)
cell.backgroundColor = UIColor.red
return cell
}
}
I used the interface builder to connect white where the cells will go to the gridViewTable. Nothing shows up at all, even tho it runs and compiles. How can I get the cells to show?
please use these code:
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 4
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath as IndexPath)
cell.backgroundColor = UIColor.red
return cell
}
You don't need the outlet for a UICollectionViewController, the class itself takes care of that.
Also, make sure that the view controller's class in Interface Builder is set to GridViewController in the Identity Inspector (3rd tab from the left in the right-most pane).

Selecting a time Interval(collection View)

enter image description here
In my project i have to select the time interval(range) as the below image.
Here is the code i am using, i have added a collectionView :
class ViewController: UIViewController , UICollectionViewDelegate, UICollectionViewDataSource{
#IBOutlet var collectionView: UICollectionView!
var time = ["0.00","1.00","2.00","3.00","4.00","5.00","6.00","7.00","8.00","9.00","10.00","11.00","12.00","1.00","2.00","3.00","4.00","5.00","6.00","7.00","8.00","9.00","10.00","11.00"]
var timeVal = ["AM","AM","AM","AM","AM","AM","AM","AM","AM","AM","AM","AM","PM","PM","PM","PM","PM","PM","PM","PM","PM","PM","PM","PM"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return time.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! timeCell
cell.timeLabel.text = time[indexPath.row]
cell.intervalLabel.text = timeVal[indexPath.row]
return cell
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSizeMake((collectionView.frame.size.width/7) , (collectionView.frame.size.height))
}
}
And i appears as the below image:
enter image description here
But i am confused how to implement that interval selection(Two time intervals).Please suggest.