In regards to a UICollectionViewDelegate
I'm using didHighlightItemAtIndexPath
as well as didSelectItemAtIndexPath
didSelectItemAtIndexPath
is working as expected. That is, when I click the remote, this code runs.
The issue is that didHighlightItemAtIndexPath
is running on click as well, when the name suggests this block will only run on highlight. Am I missing something?
The respective blocks:
func collectionView(collectionView: UICollectionView, didHighlightItemAtIndexPath indexPath: NSIndexPath) {
let node: XMLIndexer = self.xml!["ArrayOfVideo"]["Video"][indexPath.row]
let title = (node["Title"].element?.text)!
let date = (node["Date"].element?.text)!(node["SpeakerOrganization"].element?.text)!
self._title.text = title
self._date.text = date
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let node: XMLIndexer = self.xml!["ArrayOfVideo"]["Video"][indexPath.row]
let videoUrl = (node["VideoURL"].element?.text)!
self.playVideoByUrlString(videoUrl)
}
Extra Notes
For the UICollectionViewCell
I'm getting the parallax feel on the cell's image by adding the following:
// self is a UICollectionViewCell
// self.imageView is a UIImageView
self.imageView.adjustsImageWhenAncestorFocused = true
self.imageView.clipsToBounds = false
Override shouldUpdateFocusInContext
in your UICollectionView
's delegate
Method didHighlightItemAtIndexPath
called before didSelectItemAtIndexPath
because of UICollectionView
arrangement. Every time you touch cell it highlighted before you select it (for visual purpose I assume). You can see it with this example:
func collectionView(collectionView: UICollectionView, didHighlightItemAtIndexPath indexPath: NSIndexPath) {
print("highlight")
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
print("select")
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath)
let imageView = UIImageView(frame: CGRect(origin: CGPointZero, size: cell.frame.size))
imageView.image = imageFromColor(UIColor.redColor(), size: cell.frame.size)
imageView.highlightedImage = imageFromColor(UIColor.blueColor(), size: cell.frame.size)
cell.addSubview(imageView)
return cell
}
func imageFromColor(color: UIColor, size: CGSize) -> UIImage {
let rect = CGRectMake(0, 0, size.width, size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(rect)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
When I touch cell output is
highlight
select
If you want your cell be highlighted or selected only take a look at UICollectionViewDelegate's
methods shouldHighlightItemAtIndexPath
, shouldSelectItemAtIndexPath
. You can prevent selecting of highlighted cell by checking it's indexPath
inside shouldSelectItemAtIndexPath
.