Changing text size of Selected Cell in UICollectionView - ios

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()
}

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

Change cell colour when tapped UICollectionView

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])
}

Swift 4 - deselect collection view cell if already selected

I have a collection view where users can select multiple cells and send the array of index to a server for saving their selection.
everything is working except that when the collection view is created the selected items needs to be clicked two times in order to deselect them.
How can I solve this issue of double clicking ?
extension ThirdViewController: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return categoryList.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! UserCategoryCollectionViewCell
cell.categoryLbl.text = categoryList[indexPath.row]
if Defaults.hasKey(.categoryListIndices) {
if (Defaults[.categoryListIndices]?.contains(indexPath.row))! {
cell.alpha = 0.1
cell.isSelected = true
} else {
cell.alpha = 1
}
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath) as! UserCategoryCollectionViewCell
cell.alpha = 0.1
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let deselectedCell = collectionView.cellForItem(at: indexPath) as? UserCategoryCollectionViewCell
deselectedCell?.alpha = 1
print(deselectedCell?.isSelected)
collectionView.deselectItem(at: indexPath, animated: false)
}
}
first of all. add this to your viewDidLoad.
collectionView?.allowsMultipleSelection = true
and then add these two functions.
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell=collectionView.cellForItem(at: indexPath)
collectionView.selectItem(at: indexPath, animated: true, scrollPosition: [])
cell?.backgroundColor = Color.hexStringToUIColor(hex: "#F5A331")
let lbl = cell?.subviews[1] as! UILabel
lbl.textColor = Style.cellHighlightedColor
}
override func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
collectionView.deselectItem(at: indexPath, animated: true)
let cell=collectionView.cellForItem(at: indexPath)
collectionView.deselectItem(at: indexPath, animated: true)
cell?.backgroundColor = UIColor.white
let lbl = cell?.subviews[1] as! UILabel
lbl.textColor = UIColor.darkGray
}
This will do

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

Change background of specific UICollectionView cells swift

I am trying to change the background of the first two cells in my collection view i have tried this code
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = self.view.frame.width
let height = self.view.frame.height
return CGSize(width: width / 2.2 , height: height / 6)
}
override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
MyCollectionView.reloadData()
}
#IBAction func Back(_ sender: Any) {
performSegue(withIdentifier: "fourtothree", sender: nil)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return ScoreArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let Cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
let MyCell = Cell.viewWithTag(1) as! UILabel
MyCell.text = ScoreArray[indexPath.row]
if indexPath.row == 0 {
Cell.backgroundColor = UIColor.gray
MyCell.font = UIFont.boldSystemFont(ofSize: 16.0)
}
if indexPath.row == 1 {
Cell.backgroundColor = UIColor.gray
MyCell.font = UIFont.boldSystemFont(ofSize: 16.0)
}
return Cell
}
It changes the color of the first two cells which is great. however when I rotate to landscape or scroll it changes the background of different cells not just the first two.
Question: How can I change the background of only the first two cells no mater what the user does?
Since your cell get reused, you need to provide the default color for other cell.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let Cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
let MyCell = Cell.viewWithTag(1) as! UILabel
MyCell.text = ScoreArray[indexPath.row]
if indexPath.row == 0 || indexPath.row == 1 {
Cell.backgroundColor = UIColor.gray
MyCell.font = UIFont.boldSystemFont(ofSize: 16.0)
} else {
Cell.backgroundColor = UIColor.white //change with your default color
}
return Cell
}
you can change the color of ay cell but as in cellForItemAt indexPath function you are using
let Cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell that statement reuse the cell to reduce the memory usage , So to overcome this problem use
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let Cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
let MyCell = Cell.viewWithTag(1) as! UILabel
MyCell.text = ScoreArray[indexPath.row]
MyCell.font = UIFont.boldSystemFont(ofSize: 16.0)
if indexPath.row == 0 || indexPath.row == 1 {
Cell.backgroundColor = UIColor.gray
}
else {
Cell.backgroundColor = UIColor.clear
}
return Cell
}

Resources