Swift didHighlightItemAtIndexPath未按预期工作
关于Swift didHighlightItemAtIndexPath未按预期工作,swift,tvos,apple-tv,Swift,Tvos,Apple Tv,关于UICollectionViewDelegate我使用的是didHighlightItemAtIndexPath以及didSelectItemAtIndexPath didSelectItemAtIndexPath正在按预期工作。也就是说,当我单击遥控器时,此代码将运行 问题是,didHighlightItemAtIndexPath也在单击时运行,而名称表明此块仅在高亮显示时运行。我错过什么了吗 各模块: func collectionView(collectionView: UIColle
UICollectionViewDelegate
我使用的是didHighlightItemAtIndexPath
以及didSelectItemAtIndexPath
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
解决方案
由于
UICollectionView
的安排,在didSelectItemAtIndexPath
之前调用的方法didHighlightItemAtIndexPath
中重写shouldUpdateFocusInText
。每次触摸电池时,在选择它之前,它都会高亮显示(我想是为了视觉目的)。您可以通过以下示例看到:
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的
方法shouldHighlightItemAtIndexPath
,shouldSelectItemAtIndexPath
。您可以通过检查突出显示的单元格的indexPath
内部shouldSelectItemAtIndexPath
来防止选择突出显示的单元格。结果表明,我要查找的是shouldsupdatefocusincontext
。我在问题中说了“突出”,而我需要说的是“关注”。不管怎样,你的回答让我走上了解决问题的道路+我需要一个彻底的答复。