didHighlightItemAtIndexPath

didHighlightItemAtIndexPath

关于aUICollectionViewDelegateI'm usingdidHighlightItemAtIndexPathanddidSelectItemAtIndexPath
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

解决方案
覆盖shouldUpdateFocusInContext的代理中的UICollectionView

最佳答案

方法didHighlightItemAtIndexPathdidSelectItemAtIndexPath排列而在UICollectionView之前调用。每次你触摸手机的时候,它都会在你选择它之前高亮显示(我想是为了视觉目的)。您可以通过以下示例看到它:

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'smethodsshouldHighlightItemAtIndexPathshouldSelectItemAtIndexPath。通过检查突出显示的单元格在indexPath内部,可以防止选择它。

关于swift - didHighlightItemAtIndexPath无法按预期工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33573588/

10-10 18:34