Add title to CollectionViewCell - ios

I have a collection view inside a table view. What I want to do next is to set a title to each of the cells inside the collection view.
I have this code inside the ViewController
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
cell.titleLabel.text = "123"
return cell;
}
This is the cell class
class CollectionViewCell: UICollectionViewCell {
#IBOutlet weak var titleLabel: UILabel!
}
When I try to add a label inside the cell and then use this label to set the title to it nothing is displayed inside the cell.
This is my current stack:
What am I doing wrong here?

Related

Swift CollectionView ReloadData changes order for a moment

I have a very simple CollectionView which shows 4 buttons, each with the name of the Turtles. When I click any button, I want to refresh my CollectionView in case my array has increased and a 5th name was added, which should then appear as a 5th button. This basically works, but the moment I press and release any button, the button titles show my array from the last to first and then switch back to first to last.
Does anyone know why it does that and how I can stop it?
I just want to update my CollectionView every time a button was pressed. If the array should increase, let's say "Splinter", I want the Button titles to stay the same on the first four buttons and just a 5th button to be added.
Thank you very much in advance!
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
#IBOutlet weak var meineCV: UICollectionView!
let cv = CollectionViewCell()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return cv.turtles.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
cell.meinButton.setTitle(cv.turtles[indexPath.row], for: .normal)
cell.meinButton.addTarget(self, action: #selector(buttonAction(_:)), for: .touchUpInside)
return cell
}
#IBAction func buttonAction(_ sender: UIButton) {
print(sender.currentTitle!)
meineCV.reloadData()
}
}
class CollectionViewCell: UICollectionViewCell {
#IBOutlet weak var meinButton: UIButton!
let turtles: [String] = ["Leonardo", "Donatello", "Michelangelo", "Raphael"]
}
As I understood you see that the button labels are being changed at the moment of press gestures and you want to get rid of it.
Please try this:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
let turtle = cv.turtles[indexPath.row]
cell.meinButton.titleLabel?.text = turtle
cell.meinButton.setTitle(turtle, for: .normal)
cell.meinButton.addTarget(self, action: #selector(buttonAction(_:)), for: .touchUpInside)
return cell
}

Set a custom CollectionViewCells label in ViewControllers cellForItemAt indexPath method

I have been trying to set my Custom collection view cells UILabel text with the following code:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell: AlbumCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! AlbumCollectionViewCell
//set label text
cell.albumLabel?.text = self.objects[indexPath.item]
cell.backgroundColor = UIColor(r: 51, b: 51, g: 51)
return cell
}
But it results in empty labels on my cells. My cell label is returning nil but everything looks connected between my xib and collectionviewcell subclass.
Here are the xib-code file connections:
and the custom cell class:
import UIKit
class AlbumCollectionViewCell: UICollectionViewCell {
#IBOutlet weak var albumImage: UIImageView!
#IBOutlet weak var albumLabel: UILabel?
}
check delegate and dataSource protocol of collectionView is connected or not ?
if not then add this code in viewDidLoad() method
collectionView.delegate = self
collectionView.dataSource = self

Register nib with collectionview

I have FeedCell.swift and FeedCell.xib, in feedcell xib i set cell custom class to 'FeedCell'
Code in view controller viewDidLoad
collectionView.register(UINib(nibName: "FeedCell", bundle: .main), forCellWithReuseIdentifier: "feedCell")
Problem:
I want to subclass FeedCell and use that class with collectionView
Like:
class FeedCell:UICollectionViewCell {
#IBOutlet weak var showImageView: UIImageView!
#IBOutlet weak var showIconImageView: UIImageView!
}
class AppFeedCell: FeedCell {
override func awakeFromNib() {
super.awakeFromNib()
// configure cell
}
}
How to register/use FeedCell and AppFeedCell with collectionview?
If your AppFeedCell have a different UI Configuration, for example it does not bind some IBOutlets defined in FeedCell or it adds new ones not present in it's superclass, then you will have to register both Nibs (asuming you have two separated Nib files)
collectionView.register(UINib(nibName: "FeedCell", bundle: .main), forCellWithReuseIdentifier: "feedCell")
collectionView.register(UINib(nibName: "AppFeedCell", bundle: .main), forCellWithReuseIdentifier: "appFeedCell")
And then you will be able to dequeue each one when you need.
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if you_need_AppFeedCell {
let cell : AppFeedCell = collectionView.dequeueReusableCellWithReuseIdentifier("appFeedCell", forIndexPath: indexPath) as! AppFeedCell
return cell
} else
let cell : FeedCell = collectionView.dequeueReusableCellWithReuseIdentifier("feedCell", forIndexPath: indexPath) as! FeedCell
return cell
}
}
This way you may have two different Nib files even if AppFeedCell subclass FeedCell.
Otherwise, if both class and subclass share the same cell layout and outlets, then simply by casting the dequeued cell (FeedCell) to AppFeedCell should be enough, without registering another Nib, as Taras Chernyshenko pointed above.
You don't need to register separate xib for AppFeedCell. You already registered nib to your collectionView.
Just dequeue cell and cast it to needed class in your cellForItemAtIndexPath.
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath
indexPath: NSIndexPath) -> UICollectionViewCell {
if you_need_AppFeedCell {
let cell : AppFeedCell = collectionView.dequeueReusableCellWithReuseIdentifier("feedCell", forIndexPath: indexPath) as! AppFeedCell
return cell
} else
let cell : FeedCell = collectionView.dequeueReusableCellWithReuseIdentifier("feedCell", forIndexPath: indexPath) as! FeedCell
return cell
}
}
Your AppFeedCell will inherit all outlets from FeedCell so it should work as well.
In your cell xib/storyboard go to Identity inspector set class: AppFeedCell
Now in Datasource Methods of collectionview you can create cell of type AppFeedCell.
Try this.
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell : AppFeedCell = collectionView.dequeueReusableCellWithReuseIdentifier("your_reusable_identifier", forIndexPath: indexPath) as! AppFeedCell
return cell
}

how to use custom cell in collection view controller in swift 3

I am using swift 3 - I know how to show some Images in collection view with custom Cell - the problem is that I can't use custom cell in Collection View Controller
here is the collection view Controller Codes
private let reuseIdentifier = "uploadCell"
class uploadedFiles: UICollectionViewController {
var images = ["1.jpg" , "2.jpg" , "3.jpg" , "4.jpg" ]
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
//myCollectionView.sizeToFit()
//cell.sizeToFit()
cell.cellImages.image = UIImage(named: images[indexPath.row])
return cell
}
and here is the Collection view Cell Code
import UIKit
class UICollectionViewCell: UICollectionViewCell {
#IBOutlet weak var cellImages: UIImageView!
}
remember that I used "uploadCell" for identifier
For using CollectionView Custom Cell
Step 1: Create Subclass of CollectionViewCell
class ImageCell: UICollectionViewCell {
#IBOutlet weak var imgView: UIImageView!
}
Step 2: set CollectionViewCell class in StoryBoard
Step 3: reuseCollectionView Cell
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCell", for: indexPath) as! ImageCell
cell.imgView.image = self.arrMedia[indexPath.row] as? UIImage
return cell
}
UICollectionView implementation is quite interesting. You can quite simple source code and video tutorial from these link :
https://github.com/Ady901/Demo02CollectionView.git
https://www.youtube.com/watch?v=5SrgvZF67Yw
class DummyCollectionCell: UICollectionViewCell {
#IBOutlet weak var userImageView: UIImageView!
#IBOutlet weak var titleLabel: UILabel!
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DummyCollectionCell", for: indexPath) as! DummyCollectionCell
cell.titleLabel.text = nameArr[indexPath.row]
cell.userImageView.backgroundColor = .blue
return cell
}

Collection view cell doesn't recognize its outlet

I have a collection view controller ItemCollectionVC, I want to populate its cells with images, this is how I set it:
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
cell.cellImage.image = dataSourceItems[indexPath.row].image
// Error
return cell
}
Hoever, there's an error: Value of type UICollectionViewCell have no
member cellImage
The cell is of custom cell ItemCell, I set its custom class in story board, and here is ItemCell:
class ItemCell: UICollectionViewCell {
#IBOutlet weak var cellImage: UIImageView!
}
I also connected the outlet to the image view of the cell.
So I don't understand why there's an error, anybody knows?
Just one mistake that you need to define class for cell as! ItemCell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! ItemCell

Resources