2015-11-06 58 views
2

在問候一個UICollectionViewDelegate我使用didHighlightItemAtIndexPath以及didSelectItemAtIndexPathdidHighlightItemAtIndexPath不工作

didSelectItemAtIndexPath工作正常預期。也就是說,當我點擊遙控器時,這段代碼就會運行。

問題是didHighlightItemAtIndexPath也在點擊運行,當名稱暗示此塊只會在高亮顯示時運行。我錯過了什麼嗎?

的各塊:

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) 
} 

附加說明

對於UICollectionViewCell我得到的視差感覺細胞的圖像上添加以下:

// self is a UICollectionViewCell 
// self.imageView is a UIImageView 

self.imageView.adjustsImageWhenAncestorFocused = true 
self.imageView.clipsToBounds = false 

解決方案

在覆蓋shouldUpdateFocusInContextUICollectionView代表

回答

3

方法didHighlightItemAtIndexPath調用之前因爲didSelectItemAtIndexPathUICollectionView安排。每次觸摸單元格時,在選擇它之前突出顯示(爲了視覺目的,我假設)。你可以用這個例子中看到:

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 
} 

當我觸碰電池的輸出是

highlight 
select 

如果你希望你的細胞被突出顯示或選擇只看看UICollectionViewDelegate's方法shouldHighlightItemAtIndexPathshouldSelectItemAtIndexPath。您可以通過在shouldSelectItemAtIndexPath內檢查indexPath來防止突出顯示的單元格被選中。

+0

原來我在找的是'shouldUpdateFocusInContext'。我在我的問題中「強調」什麼時候我需要說的是「重點」。無論如何,你的回答讓我走上了解決問題的道路。 +1進行徹底回覆。 – Jacksonkr