UICollectionView scrollToItem() 不起作用
来源:互联网 发布:淘宝达人账号怎么取消 编辑:程序博客网 时间:2024/06/05 20:49
今天在做东西的时候又遇到这样一个问题,把 UICollectionView
嵌套在 UIViewController
中使用,在想用 scrollToItem()
方法时,怎么都不起作用,找了半天,原来是 位置属性设置错了。
原本的代码是
// viewDidLoad 里面navigationItem.rightBarButtonItem = UIBarButtonItem(title: "定位", style: .plain, target: self, action: #selector(locatSelectedCell))// 外部方法func locatSelectedCell () { if let lastSelectedIndex = lastSelectionID { DispatchQueue.main.async { self.thingsCollectionView.scrollToItem(at: IndexPath(item: lastSelectedIndex, section: 0), at: .centeredHorizontally, animated: true) } } }
错误就出在 at: .centeredHorizontally
,因为我的 collectinoView
的方向是纵向的,所以应该是 at: .centeredVertically
纵向居中显示对应的 cell
func locatSelectedCell () { if let lastSelectedIndex = lastSelectionID { DispatchQueue.main.async { self.thingsCollectionView.scrollToItem(at: IndexPath(item: lastSelectedIndex, section: 0), at: .centeredVertically, animated: true) } } }
阅读全文
0 0
- UICollectionView scrollToItem() 不起作用
- UICollectionView
- UICollectionView
- UICollectionView
- UICollectionView
- UICollectionView
- UICollectionView
- UICollectionView
- UICollectionView
- UICollectionView
- UICollectionView
- UICollectionView
- UICollectionView
- UICollectionView
- UICollectionView
- UICollectionView
- UICollectionView
- UICollectionView
- Java基础技术核心归纳(二)
- 如何提高讲话思维逻辑能力?
- Java基础之集合框架--TreeSet
- 顺序三元组
- 浅谈Attention-based Model【源码篇】
- UICollectionView scrollToItem() 不起作用
- 初识hadoop
- QT+ffmpeg打造跨平台多功能播放器
- 关于数据库的事务处理
- Linux Shell脚本攻略1:小试牛刀(3)
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛
- 回忆自己的大学---黑龙江科技大学
- 远程过程调用(RPC)详解(发展历史)
- 【C++】【学习笔记】向自定类中添加 combine 和 isbn 成员