- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个自定义的 Collection View 布局,它似乎会产生一些错误。或者也许我错过了什么。布局看起来像这样,来自 here :
我的问题是我无法将居中单元格的 indexPath 传递给粉红色按钮,调用 centerCellIndexPath
会产生 nil。我还尝试在布局本身中预选单元格,如下所示:
override open func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
// item's setup ......
// ...................
let finalPoint = CGPoint(x: newOffsetX, y: proposedContentOffset.y)
// Select cell in collectionView by Default
let updatePoint = CGPoint(x: newOffsetX, y: 180)
let indexPath = collectionView!.indexPathForItem(at: updatePoint)
self.collectionView!.selectItem(at: indexPath, animated: false, scrollPosition: [])
return finalPoint
}
但它不起作用。我必须通过向上滑动来手动选择单元格,这对用户来说非常不清楚。我应该如何在不点击和滑动的情况下选择单元格?
任何建议将不胜感激
UPD:经过 Vollan 的一夜未眠,终于成功了
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let position = 2 * scrollView.contentOffset.x / collectionView.superview!.frame.width
selectedItem = Int(round(position))
}
UPD:但是最好的只会变得更好。从链接到 Swift 版本的转换答案
This would be better code because it's cleaner and easier to read, all the content offset calculation is superfluous:
NSIndexPath *centerCellIndexPath =
[self.collectionView indexPathForItemAtPoint:
[self.view convertPoint:[self.view center] toView:self.collectionView]];
This would also be the correct representation of what you're actually trying to do: 1. Taking the center point of your viewcontroller's view - aka visual center point 2. convert it to the coordinate space of the view you're interested in - which is the collection view 3. Get the indexpath that exist at the location given.
所以需要两行:
let centerPoint = self.view.convert(view.center, to: collectionView)
let indexPath = collectionView.indexPathForItem(at: centerPoint)
-> save media[indexPath.row]
这基本上就是 Sachin Kishore 的建议,但一开始我不理解他
我有点喜欢动态 indexPath 的想法,但它需要一些舍入,这是不可靠的。所以我认为 convert
和 indexPathForItem
在这里最好
最佳答案
如果您要将逻辑放入 scrollViewDidScroll
中,您可以动态更新 indexPath
。
let position = scrollView.contentOffset.x/(cellSize+spacing)
currenIndexPath.item = position
这应该始终为您提供当前单元格的 indexPath。
关于ios - 默认情况下在 collectionView 中滚动时选择单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49891391/
我在 Storyboard 中有一个 collectionView,如下所示: 注意约束:Collection View.top = Search Bar.bottom 我不想要该行上方的 Colle
我有一个包含两个部分的 Collection View ,每个部分都有一个项目。部分中的单元格包含 Collection View ,我需要使高度单元格适合内容 Collection View 。 我
我见过将 Collection View 嵌套在 TableView 中的解决方案,但对于我的应用程序,我需要有 2 个 Collection View ,因为这样可以更轻松地执行其他操作。 因此,让
感谢帮助。 如上所示,CollectionView ItemSize.height 比 CollectionView.height 大。我只是在 viewDidLayoutSubviews() 中设置
我有一个collectionview(A),它可以作为“卡片 View ”来滑动。在最后一个卡/单元格中,我有另一个 Collection View (B)来显示更多单元格。到目前为止,一切都很好!一
我正在寻找一种方法,当您滚动到 collectionView 中的另一个单元格时,可以更改 imageView 中的图像。 因此,当滚动单元格的 (1) 时,我想更改图像 (2)。 创建此内容的最佳方
我的 NSCollectionView 项目集成了另一个 NSCollectionView。一切都很好,但在第一个 View 中选择项目时就不行了。 当我设置collectionView1.isSel
我是一名 Android 开发者,正在尝试学习 iO 开发。 我正在尝试在水平分页滚动器内实现垂直滚动 UI(就像 ViewPager 内的 RecyclerView)。 我知道我应该使用嵌套的 UI
我在collectionView中有一个collectionView,我希望能够选择第二个collectionView中的一个单元格来执行到另一个ViewController的segue。 目前,当我
我有这两个代表: #pragma mark = UICollectionViewDataSource - (NSInteger)collectionView:(UICollectionView*)co
嘿,我正在尝试在我的动态 Collection View 单元格之后添加一个静态 Collection View 单元格。例如在我的屏幕截图中,在“Study Grind Edition” Colle
是否可以通过在 Collection View Cell .swift 文件中编写代码来阻止我的 Collection View 滚动。我希望能够在用户点击单元格中的按钮时停止滚动,然后在再次按下按钮
我有一个用例,我想要呈现一组collectionViews(或复合 View )。使用 marionette.js 执行此操作的最佳方法是什么? 我的模型结构如下- A | |-- Collectio
我有一个 View Controller ,其中有 tableView 和 Collection View 。我为 collectionView 和 tableView 设置了相同的高度。 前半部分是
在 Visual Studio 2017 中,尝试存档 Xamarin Android 项目时,成功构建后出现以下错误: 无法创建应用程序存档“MyArchive”。这种类型的 CollectionV
我对 Collection View 完全陌生,请帮助我在 Collection View 中垂直和水平滚动单元格: viewcontroller.h #import @interface View
我在一个表格 View 单元格中有两个不同的 collectionView。原因是因为我试图找到实现一个水平 collectionView 和一个显示不同数据的垂直 collecitonView 的最
我想在另一个 collectionview 标题中实现一个 Collection View 幻灯片,但是当我尝试连接 uicollectionview 的导出时,我收到一条错误消息,指出导出无法连接到
您好,我已经为这个问题苦苦挣扎了一段时间,一直想不出来。 我想做的是,在 collectionview 单元格内按下一个按钮,然后执行到下一个单元格的转换。 这是我想在 collectionview
在 collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayo
我是一名优秀的程序员,十分优秀!