gpt4 book ai didi

swift - didHighlightItemAtIndexPath 没有按预期工作

转载 作者:可可西里 更新时间:2023-11-01 01:03:17 25 4
gpt4 key购买 nike

关于 UICollectionViewDelegate 我正在使用 didHighlightItemAtIndexPath 以及 didSelectItemAtIndexPath

didSelectItemAtIndexPath 按预期工作。也就是说,当我点击 Remote 时,这段代码就会运行。

问题是 didHighlightItemAtIndexPath 也在点击时运行,顾名思义这个 block 只会在高亮时运行。我错过了什么吗?

各自的 block :

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

解决方案

在您的UICollectionView委托(delegate)中覆盖shouldUpdateFocusInContext

最佳答案

方法 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 的 方法shouldHighlightItemAtIndexPathshouldSelectItemAtIndexPath。您可以通过检查 shouldSelectItemAtIndexPath 中的 indexPath 来防止选择突出显示的单元格。

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com