facebookarchive / AsyncDisplayKit

Smooth asynchronous user interfaces for iOS apps.
http://asyncdisplaykit.org
Other
13.4k stars 2.2k forks source link

collectionView nodeBlockForItemAtIndexPath, indexPath is not valid #2330

Closed haashem closed 8 years ago

haashem commented 8 years ago

so here it is:

func collectionView(_ collectionView: ASCollectionView, nodeBlockForItemAt indexPath: IndexPath) -> AsyncDisplayKit.ASCellNodeBlock{

        return {
            return ASCellNode(viewControllerBlock: {[unowned self] () -> UIViewController in
                let menuViewController: RestMenuViewController2 = UIStoryboard.MainStoryboard.instantiateViewController(withIdentifier: "RestMenuViewController2") as! RestMenuViewController2
                // add created menuVC to viewControllers array
                self.viewControllers.append(menuViewController)
                let category = self.categories[indexPath.row]
                menuViewController.category = category
                return menuViewController
        }
    }

when ASColelctionView asynchronously asks for a new ASCellNode, indexPath rapidly gets changed, but still ASCellNodeBlock is not called, so when I want to access an object in the block I don't get correct object because IndexPath has changed!

what's your Idea? should the block hands me the indexPath, or in some way I should persist the sequence of indexPaths?

maicki commented 8 years ago

@hashemp206 Best practice is to capture the indexPath or row outside of the ASCellNodeBlock and use it inside:

func collectionView(_ collectionView: ASCollectionView, nodeBlockForItemAt indexPath: IndexPath) -> AsyncDisplayKit.ASCellNodeBlock{
        let row = indexPath.row;
        return {
            return ASCellNode(viewControllerBlock: {[unowned self] () -> UIViewController in
                let menuViewController: RestMenuViewController2 = UIStoryboard.MainStoryboard.instantiateViewController(withIdentifier: "RestMenuViewController2") as! RestMenuViewController2
                // add created menuVC to viewControllers array
                self.viewControllers.append(menuViewController)
                let category = self.categories[row]
                menuViewController.category = category
                return menuViewController
        }
    }