1
votes

I have 2 buttons and 1 collectionView on a ViewController. On clicking one button the collectionView's data source changes and It is reloaded to change the options shown in the collectionView's cells, and on clicking the other button different options are shown. I want to change the font of the text in the label(this label is present inside the cell) of the cell selected by the user.So, for that I did the following

var previouslySelectedCell: IndexPath? = IndexPath(row: 0, section: 0)
var leftOutPreviouslySelectedCell: IndexPath?

In cellForItemAt:

cell?.frameNameLabel.font = UIFont.Book16    
if indexPath == previouslySelectedCell {
cell?.frameNameLabel.font = UIFont.Medium16
}

In didSelectItemAt:

    if previouslySelectedCell != nil {
      if let previousCell = collectionView.cellForItem(at: previouslySelectedCell!) as? FramePhotoCell {
   previousCell.frameNameLabel.font = UIFont.Book16
    } else {
     leftOutPreviouslySelectedCell = previouslySelectedCell
           }
  }
  let cell = collectionView.cellForItem(at: indexPath) as! FramePhotoCell
  cell.frameNameLabel.font = UIFont.Medium16
  previouslySelectedCell = indexPath

In willDisplay:

if indexPath == leftOutPreviouslySelectedCell {
   (cell as! FramePhotoCell).frameNameLabel.font = UIFont.Book16
   leftOutPreviouslySelectedCell = nil
        }

Problem with this approach is that if I select a cell in one collectionView an then when I tap on a button which changes its dataSource and a new reloaded collectionView with new data appears; There too the previously selected cell (selected in previous version of collectionView) seems to be selected/highlighted, as that cell's IndexPath is there in the previously selected Cell. I don't want to erase values of previously selected cell on switching to the other collectionView, as if a user selects one cell and then moves to the other version of collectionView, and then comes back to the previous version of collection view by tapping on the 2 buttons given, The previously selected cell should still be in the selected mode. What should I do?

1
apply check on dataSource if its from Button1 then change font as your requirement else change font for 2nd one and also save state of which button is presedAsim Ifitkhar Abbasi
I don't get the else part in didSelectItemAt. because previouslySelectedCell will be nil in that scope.hardik parmar
@hardikparmar collectionView.cellForItem(at: previouslySelectedCell!) return a value only if the cell with that particular IndexPath is currently visible, otherwise it return nilpravir
I get that. I was asking about the else part. setting leftOutPreviouslySelectedCell = previouslySelectedCell will set leftOutPreviouslySelectedCell to nil.hardik parmar
@hardikparmar if user taps on a cell that is currently not visible (but is among those cells that have not been released from the memory [ if there are three complete cells visible on screen then 5 cells will be kept alive in memory. 3 will be the visible ones, and the other 2 will be used as spare cells, one on the left and the other one on the right of those 3 visible cells. Those 5 cells keep getting recycled/reused. (I am considering the collectionView to be horizontal in this example.) the other 2 will not be visible as obviously, they are present out of the collectionView's width ] ),pravir

1 Answers

0
votes

Take an array as arrSelectedIndex

In OnBtnClick

if index is not in array{
 1. add index in array
 2. reload your data
}
else if index is in array{
 1. remove index in array
 2. reload your data
}

In cellForRow

cell.btn.tag = indexPath.row

1. check cuurent indexPath is in the `arrSelectedIndex` or not.
2. if step1 true{
  show your hightlighted data
}
else{
  show default data
}

hope this helps you.