问题描述
我需要指定哪个单元格应该获得焦点.
I need to specify which cell should receive focus.
根据Apple文档,如果remembersLastFocusedIndexPath
属性为false
,或者由于先前没有单元格而没有保存的索引路径,则应调用indexPathForPreferredFocusedView
委托方法.
According to Apple Documentation the indexPathForPreferredFocusedView
delegate method should be called if the remembersLastFocusedIndexPath
property is false
, or if there is no saved index path because no cell was previously focused.
就我而言,我在UIViewController
中使用集合视图,并将remembersLastFocusedIndexPath
设置为false
,但未调用indexPathForPreferredFocusedView
.
In my case I am using a collection view in a UIViewController
and setting remembersLastFocusedIndexPath
to false
but indexPathForPreferredFocusedView
is not being called.
如何解释这种行为?
推荐答案
函数indexPathForPreferredFocusedView
是UICollectionViewDelegate的一部分,因此可能是未将委托分配给您的collectionView.
The function indexPathForPreferredFocusedView
is part of UICollectionViewDelegate, so it might be that the delegate was not assigned to your collectionView.
或者,问题可能也出在焦点环境上,而不考虑您的UICollectionView.
Or, the problem might also be on the focus environment, not taking into account your UICollectionView.
作为参考,这里有一个具有5个单元格的简单collectionView的示例,默认情况下最初选择了一个位于中心的单元格
As a reference, here you have an example of a simple collectionView with 5 cells, having the one in the center initially selected by default
import UIKit
class ViewController: UIViewController {
private var collectionView: UICollectionView!
private var items = ["One", "Two", "Three", "Four", "Five"]
override var preferredFocusEnvironments: [UIFocusEnvironment] {
return [collectionView]
}
override func viewDidLoad() {
super.viewDidLoad()
collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: UICollectionViewFlowLayout())
collectionView.remembersLastFocusedIndexPath = false
MyCell.register(in: collectionView)
view.addSubview(collectionView)
collectionView.dataSource = self
collectionView.delegate = self
}
}
extension ViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return items.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MyCell.reuseIdentifier, for: indexPath) as! MyCell
cell.titleLabel.text = items[indexPath.row]
return cell
}
}
extension ViewController: UICollectionViewDelegate {
func indexPathForPreferredFocusedView(in collectionView: UICollectionView) -> IndexPath? {
return IndexPath(row: 2, section: 0)
}
}
class MyCell: UICollectionViewCell {
static var reuseIdentifier: String { return String(describing: self) + "ReuseIdentifier" }
var titleLabel: UILabel!
public static func register(in collectionView: UICollectionView) {
collectionView.register(MyCell.self, forCellWithReuseIdentifier: MyCell.reuseIdentifier)
}
override init(frame: CGRect) {
super.init(frame: frame)
titleLabel = UILabel(frame: bounds)
backgroundColor = .blue
contentView.addSubview(titleLabel)
}
override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
backgroundColor = isFocused ? .red : .blue
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
这篇关于没有调用indexPathForPreferredFocusedView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!