ios – Detect when UITableViewCell goes off the display

[ad_1]

I wanted to get some information from the cell because it was scrolled off of the display. I used @Mr.T’s reply nonetheless it would not state easy methods to get the information.

Say for instance the title of the cell class that I am utilizing is MyCell and it has an information mannequin in it named MyModel with a property of postId. I initially set that information in cellForItem:

var datasource = [MyModel]()

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! MyCell

    cell.myModel = datasource[indexPath.item] // a person occasion of MyModel from the array

    print("cellForItem - indexPath.merchandise: ", indexPath.merchandise) // if the was the very first cell approaching it might print 0
    print("postId: ", cell.myModel.postId) // possibly the postId is qwerty

    return
}

To get some information from the cell as it’s scrolled off of the display:

func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {

    guard let myCell = cell as? MyCell else { return } // You should solid the cell from the strategy param to your cell sort which for me is MyCell

    print("didEndDisplayingCell - indexPath.merchandise: ", indexPath.merchandise) // if this was the very first cell scrolling off it ought to print 0
    print("postId: ", myCell.myModel.postId) // the postId needs to be qwerty
}

One of the best ways to check that is so as to add a small quantity of cells to your collectionView, like first 2 cells, then afterward 3 cells, then afterward 4 cells. Then simply scroll off the very first cell and see what’s printed out. Do it for every cell. The indexPath.merchandise and postId ought to each match for cellForItem and didEndDisplaying.

[ad_2]

Leave a Reply