2015-09-26 212 views
4

我有一个标准的水平布局的集合视图。在呈现视图控制器然后解除视图控制器时,即使最后一个聚焦单元不是那个,收集视图重置也会回到第一个单元格。remembersLastFocusedIndexPath不工作在UICollectionView

我设置

self.collectionView.remembersLastFocusedIndexPath = YES; 

什么奇怪的是,当我在我的导航控制器上推视图控制器这只是发生。

所以,如果我做

[self.navigationController pushViewController:controller animated:YES]; 

,解散,remembersLastFocusedIndexPath不能正常工作。

但是,如果我:

[self presentViewController:controller animated:YES completion:nil]; 

然后,它按预期工作。

任何想法为什么它不会通过导航控制器?

+1

不要浪费时间在这个。这根本不适用。如果你想保持你选择的单元格使用presentViewController。 –

+0

顺便说一句,[self performSegueWithIdentifier:YOUR_SEGUE_ID sender:self] does not working with remembersLastFocusedIndexPath –

+1

我看到同样的事情。尽管我使用segues。 'remembersLastFocusedIndexPath = true'在我使用“Present Modally”segue种类时起作用,但当我使用“Show(例如Push)”种类时不起作用。我已经向Apple提交了一份错误报告,如果我收到任何回应,它会通知您。看起来像一个错误。 P.S.这仅在使用添加到“UIViewController”的“UICollectionView”时才会发生。使用'UICollectionViewController'时我没有问题。在我的情况下,切换到“UICollectionViewController”不是一个选项。 –

回答

1

默认情况下,系统会记住位置。您应该通过将remembersLastFocusedIndexPath设置为false来解决此问题。

你也可以实现UICollectionViewDelegate方法: func indexPathForPreferredFocusedViewInCollectionView(collectionView: UICollectionView) -> NSIndexPath?

的文档告诉我们:

此委托方法的功能相当于在UIFocusEnvironment协议重写UICollectionView类的preferredFocusedView方法。如果集合视图的remembersLastFocusedIndexPath方法设置为YES,则此方法将定义首次聚焦集合视图时获得焦点的索引路径。

但是对于你的问题,将remembersLastFocusedIndexPath设置为false应该修复它。

+0

Nope ,这并没有解决它 – Banana

+0

这帮助我指出了如何设置'remembersLastFocusedIndexPath'的“初始设置”...谢谢!:) –

5

什么工作对我来说是确保在该的viewController是preferredFocusView收集观点:

override weak var preferredFocusedView: UIView? { 
    return collectionView 
} 

在此之后remembersLastFocusedIndexPath似乎工作。

+0

这对我有用 – Daniel

2

确保您不会在您的collectionView或TableView的viewWillAppear上重载dataData()。否则记住Focused将被重置为默认值(对于我的情况下的集合视图,它是中心可见单元格)

+0

在viewWillAppear中做一个reloadData()会导致一个不同的在重新加载单元格和被重用的单元格在重新加载之后再次获得焦点的时候,这是有道理的,所以,谢谢,当我删除reloadData()时,在我看来,我有适当的细胞集中。 –

0

您也可以尝试实现类似的行为。只记得最后聚焦的细胞func collectionView(collectionView: UICollectionView, didUpdateFocusInContext context: UICollectionViewFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator)并使用这个信息在func collectionView(collectionView: UICollectionView, canFocusItemAtIndexPath indexPath: NSIndexPath) -> Bool