1

I want get values of all cells in my collection view but I have differents sections and my collection view is it not so all cells visible , when I try obtain the values , the cell visible obtain values with success but when I try obtain the values for the rest cells show the error:

unexpectedly found nil while unwrapping an Optional value

My code is

func getValueCells() {
    for section in 0..<numberOfYears {
        for row in 0..<3 {
            let index = IndexPath(row: row, section: section)

            let cell = myCollection!.cellForItem(at: index) as! CircularCell
            print(cell.lblPercent.text)
        }
    }
}

The index is correct , any help?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Alejandro
  • 315
  • 5
  • 12
  • 2
    Possible duplicate of [What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?](https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – Tamás Sengel Sep 13 '17 at 15:38
  • Its due to Cell Reusability . If you want to get all data then why dont u use this using datasource , like using array or dintionary – Shobhakar Tiwari Sep 13 '17 at 15:39

2 Answers2

3
if let cell = myCollection!.dataSource?.collectionView(self.collectionView, cellForItemAt: index) as? CircularCell {
  //access cell here
}

CollectionView will not be able to return the cell which is not in visible range as the cell might have been reused.

You should ask the data source for the cell rather than collectionView itself.

Hope it helps

EDIT:

Though above answer explains how to get the access to the cell which is outside the visible indexPath your code still will not work

print(cell.lblPercent.text)

You will not be able to access the content of label/textField which is inside the cell unless u have implemented

override func prepareForReuse() {
   //save the labels text somewhere in a variable or model
}

in CircularCell and made sure u initialize all the labels/textFields with text in cellForRowAtIndexPath you have implemented.

Values of the UI elements inside the cell are not persisted when cell gets reused. Its ur responsibility to ensure the same

Sandeep Bhandari
  • 19,999
  • 5
  • 45
  • 78
  • 1
    So , is not possible get values of all cels because the reuse cells don't have the correct values or the actual values not? The solution is save the values and access to values not? Do you other solution without save the values in bd or other same thing? – Alejandro Sep 13 '17 at 15:54
  • @alejandro : Unfortunately I don't see any other solutions. The values that are dynamically inserted to UI components in cell like text entered by user, range set by user in slider stuff like these should be saved before the cell gets reused. None of the UI components retain their values when cell gets reused. They take values whatever u set in cellForRowAtIndexPath – Sandeep Bhandari Sep 13 '17 at 15:57
  • As I mentioned prepareForReuse() is one such place where u can access the values of UIComponents one last time there are other ways to figure out when will cell gets reused but most of them I find kind of hacks. I prefer writing protocol in cell and confirm it in TableView/collectionView and triggering the protocol in prepareForReuse() and pass the data and indexPath of cell to corresponding delegate and expect the delegate to update data source with user input – Sandeep Bhandari Sep 13 '17 at 15:59
0

Due to the reusable behavior of UICollectionView and UITableView cells that is not possible to get the values of invisible cells,

Because, the cells will lose their reference whenever it becomes invisible.

You can get the values from the visible cell like above answer.

Salman Ghumsani
  • 3,647
  • 2
  • 21
  • 34