Cell selection issue in CollectionView

I have a UICollectionView that presents a horizontally scrolling row of cells.


The collection view is set up to allow single selection, and the cells are set up to change background color when selected.


If I tap on a cell, it always shows the selected appearance, and the previously selected cell always changes back to the unselected appearance.


I also want to programatically select cells in my code, using selectItemAtIndexPath:


[collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionCenteredHorizontally];


The problem I am having is that this doesn't always cause the cell to adopt the selected appearance. Specifically, if the cell I am selecting is not visible, it doesn't get the selected appearance, although it does scroll into the visible area. If the cell is already visible, it works fine.


I have tried a large number of hacks to fix this, such as manually setting the cell's selected property, but nothing seems to work.