Change cell colour when tapped UICollectionView - ios

I'm working on an app with a UICollectionView, and I use didSelectItemAt to change a boolean's value, but I need to make the cell colour change when I tap on it. This is the didSelectItemAt that I am using:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotoCell", for: indexPath) as! PhotoCell
let caseName = OLLData.list[indexPath.item].image
print(caseName, OLLData.list[indexPath.item].selected)
OLLData.list[indexPath.item].selected = !OLLData.list[indexPath.item].selected
if OLLData.list[indexPath.item].selected == true {
cell.imageView.backgroundColor = UIColor.yellow
print("Changed Colour to Yellow")
}
else {
cell.imageView.backgroundColor = UIColor.clear
print("Changed Colour to Clear")
}
}
Am I doing this the right way, or is there another?

Replace
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotoCell", for: indexPath) as! PhotoCell
with
let cell = collectionView.cellForItem(at: indexPath) as! PhotoCell
Or better after you apply the changes to the model reload that indexPath
collectionView.reloadItems(at:[indexPath])
dequeueReusableCell shouldn't be used out of cellForItemAt
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotoCell", for: indexPath) as! PhotoCell
cell.imageView.backgroundColor = OLLData.list[indexPath.item].selected ? UIColor.yellow : UIColor.clear
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let caseName = OLLData.list[indexPath.item].image
print(caseName, OLLData.list[indexPath.item].selected)
OLLData.list[indexPath.item].selected = !OLLData.list[indexPath.item].selected
collectionView.reloadItems(at:[indexPath])
}

Related

Deselect default selected cell on selecting any other cell in UICollectionview

Using UICollectionView to display calendar, I have made today's date selected by default and want to deselect that when any other date selected. Below is my code:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CalenderCell", for: indexPath) as! CalenderCollectionViewCell
cell.lblDayName.text = arrCalendarDays[indexPath.item]
cell.lblDate.text = arrCalendarOnlyDate[indexPath.item]
if indexPath.item == 0 {
if !isTodayDate {
cell.isSelected = true
} else {
cell.isSelected = false
}
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
isTodayDate = true
// Tried below code but not working
/* collectionViewWeekCalendar.indexPathsForSelectedItems?
.forEach {
self.collectionViewWeekCalendar.deselectItem(at: $0, animated: false) }
*/
let selectedCell:UICollectionViewCell = collectionViewWeekCalendar.cellForItem(at: indexPath)!
selectedCell.isSelected = true
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let selectedCell:UICollectionViewCell = collectionViewWeekCalendar.cellForItem(at: indexPath as IndexPath)!
selectedCell.isSelected = false
}
Problem is that, when selecting any other date, today's selected date is not getting deselect. Please guide.
the hacky and easiest way (i think)
use selected index
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CalenderCell", for: indexPath) as! CalenderCollectionViewCell
cell.lblDayName.text = arrCalendarDays[indexPath.item]
cell.lblDate.text = arrCalendarOnlyDate[indexPath.item]
if sellectedIndex == indexPath {
cell.isSelected = true
} else {
cell.isSelected = false
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
selectedIndex = indexPath
collectionView.reloadData()
}
and at the start,
set selectedIndex = today date Index

Value of type 'UICollectionViewCell?' has no member 'contentImage'

I have two custom UICollectionViewCells(AddImageCollectionViewCell, ItemCollectionViewCell) which I am loading depending on indexpath. Here is my code for the cellForItemAtIndexpath-
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var cell :UICollectionViewCell!
if indexPath.row == 0{
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "addImageCell", for: indexPath) as! AddImageCollectionViewCell
}
else{
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ItemCell", for: indexPath) as! ItemCollectionViewCell
cell.contentImage = self.droppedItemList[indexPath.row] //error here
}
return cell
}
I am getting the error as "Value of type 'UICollectionViewCell?' has no member 'contentImage'". Why is my cell in the else clause is not cast to "ItemCollectionViewCell" type.
I know, I must be doing something very foolish. I would be really grateful if someone can point me to the right direction.
You are declaring cell as basic type UICollectionViewCell, that's the reason. Return the cells separately
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.row == 0 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "addImageCell", for: indexPath) as! AddImageCollectionViewCell
return cell
} else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ItemCell", for: indexPath) as! ItemCollectionViewCell
cell.contentImage = self.droppedItemList[indexPath.row]
return cell
}
}

Changing text size of Selected Cell in UICollectionView

I am trying to change the text size of collection view cell label and works a little bit. When I select item index wise(cell 0, cell 1, cell 2....) it works for me But when I try to select item on random index(like: cell 0 to cell 3) then my app crashes
I'm trying this code:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let idxPath = IndexPath(item: indexPath.item, section: 0)
let cell = collectionView.cellForItem(at: idxPath)
let myCell = collectionView.cellForItem(at: idxPath) as! MenuCell
cell?.isSelected = true
myCell.isSelected = true
myCell.title.font = UIFont.systemFont(ofSize: 16)
collectionView.scrollToItem(at: idxPath, at: UICollectionViewScrollPosition.centeredHorizontally, animated: true)
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let idxPath = IndexPath(item: indexPath.item, section: 0)
let myCell = collectionView.cellForItem(at: idxPath) as! MenuCell
myCell.isSelected = false
myCell.title.font = UIFont.systemFont(ofSize: 13)
}
It's working in series cell selection
enter image description here
But when i select cell randomly
enter image description here
Thanks, and sorry for my bad english
Because you recreate IndexPath and then making force unwrap for the cell.
If you have to change font just for first section, so try to change code like this:
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
if indexPath.section == 0 {
let myCell = collectionView.cellForItem(at: indexPath) as? MenuCell
myCell?.isSelected = false
myCell?.title.font = UIFont.systemFont(ofSize: 13)
}
}
Try this...
var selectedCell = [Int]()
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: yourCellIdentifier, for: indexPath) as! MenuCell
//Code for change font size
if selectedCell.contains(indexPath.cell){
cell.title.font = UIFont.systemFont(ofSize: 16)
}else{
cell.title.font = UIFont.systemFont(ofSize: 13)
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if selectedCell.contains(indexPath.item){
selectedCell.remove(at: selectedCell.index(of: selectedCell.contains[indexPath.item])!)
}else{
selectedCell.append(indexPath.item)
}
collectionView.reloadData()
}

ios UICollectionView cell selecting and deselecting issue

Im using UIcollection view as my tabbar
when I scroll collection view horizontally previous selected cell will not deselect when i select new one
this is my code to change colour when i select a cell and deselect a cell
var selectedIndexPath : IndexPath = []
func collectionView(_ collectionView: UICollectionView,
didSelectItemAt indexPath: IndexPath) {
if let cell = collectionView.cellForItem(at: indexPath) as?
BottomCollectionViewCell {
cell.contentView.backgroundColor = UIColor.orange
cell.backgroundColor = UIColor.orange
}
if let preViousSelectedcell = collectionView.cellForItem(at:
selectedIndexPath) as? BottomCollectionViewCell {
preViousSelectedcell.contentView.backgroundColor=UIColor.purple
preViousSelectedcell.backgroundColor = UIColor.purple
}
selectedIndexPath = indexPath
}
while scrolling cells are reused that time cellForItemAt will call so you need to change some modification in your code
func collectionView(_ collectionView: UICollectionView,
didSelectItemAt indexPath: IndexPath) {
selectedIndexPath = indexPath
YOUR_COLLECTION_VIEW.reloadData()
}
and add below lines inside your collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath)
if indexPath == selectedIndexPath {
cell.contentView.backgroundColor=UIColor.purple
cell.backgroundColor = UIColor.purple
} else {
cell.contentView.backgroundColor = UIColor.orange
cell.backgroundColor = UIColor.orange
}
Hope this will help you

Add individual cell to UiCollectionView

I'm attempting to create a custom cell at the end of my UICollectionViewCell. The last item should be an "custom cell" so the user can add another record. Tried some solutions but without success. My code so far seems like this:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! EquipamentosCollectionViewCell
if (indexPath as NSIndexPath).row == 0 {
cell.imgEquip?.image = UIImage(named: "novavisita")
return cell
}else{
cell.backgroundColor = UIColor.green
return cell
}
}
I also tried this solution, but it`s not how I want it to work.
The problem is that it's replacing the first.
Any suggestions of how to do that?
Then add that cell at last:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! EquipamentosCollectionViewCell
if (indexPath as NSIndexPath).item == YOUR_COLLECTION.count - 1{
//Add New Record Cell
cell.imgEquip?.image = UIImage(named: "novavisita")
return cell
}else{
cell.backgroundColor = UIColor.green
return cell
}
}

Resources