gpt4 book ai didi

ios - UICollectionView 从右向左滚动(反向)

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

我想要一个从右到左滚动的 UICollectionView,即当它在加载后出现在屏幕上时, Collection View 应该首先显示最右边的单元格/项目,然后将其余的添加到左边。我已经尝试了提供的解决方法 here但是,如果我在 viewWillAppear: 中调用它,我会得到:

*** Assertion failure in -[UICollectionViewData layoutAttributesForItemAtIndexPath:], /SourceCache/UIKit/UIKit-2372/UICollectionViewData.m:485

如果我滚动到 viewDidAppear: 中的最后一个项目,它工作正常,除了现在用户首先看到左边的项目,然后滚动到最后一个项目。还尝试在 UICollectionView 上使用 contentOffset 属性(因为它是 UIScrollView 的子类),但此参数也仅在 viewWillAppear:viewDidAppear:< 之间的某个时间设置

还有其他选择吗?我想我可以尝试子类化 UICollectionViewFlowLayout 并从右到左布置单元格,但我有点急于进入那个领域。

这是我的做法:

- (void)_scrollToTheRight
{
NSIndexPath *lastIndexPath = [self.fetchedResultsController indexPathForObject:self.fetchedResultsController.fetchedObjects.lastObject];
if (lastIndexPath)
{
[self.collectionView scrollToItemAtIndexPath:lastIndexPath atScrollPosition:UICollectionViewScrollPositionRight animated:NO];
}
}

每个值是:

lastIndexPath
(NSIndexPath *) $0 = 0x1f1754a0 <NSIndexPath 0x1f1754a0> 2 indexes [0, 21]

(NSInteger)[self.fetchedResultsController.fetchedObjects count]
(NSInteger) $3 = 22 [no Objective-C description available]

(NSInteger)[self.collectionView numberOfItemsInSection:0]
(NSInteger) $2 = 22 [no Objective-C description available]

最后一点: Collection View Controller 是从 View Controller 的容器 View (iOS 6 Storyboard中的新功能)加载的,该 View Controller 由 UIPageViewController 放在屏幕上。

谢谢。

编辑 1:所以我认为问题是使用我的 Storyboard布局(UICollectionViewController 使用新的容器 View 嵌入)导致了问题。 enter image description here因为在应用程序的其他地方我也嵌入了 View Controller (一个普通的 UITableViewController)viewDidAppear: 在 View 实际出现之前被调用。我怀疑这个设置没有正确地通知每个 subview Controller 他们应该加载和布局他们的 View 。

最佳答案

从iOS 9开始可以使用

collectionView.semanticContentAttribute = .forceRightToLeft

scrollDirection = .horizo​​ntal 并且 itemSize 填满整个高度(即只有一行项目)时从右向左滚动

关于ios - UICollectionView 从右向左滚动(反向),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13958543/

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