【发布时间】:2017-10-04 16:27:12
【问题描述】:
我有一个水平的按钮集合视图。当其中一个被点击时,用户被带到另一个视图控制器。很多时候并不是所有的按钮都是可见的,所以我想要的是让选定的按钮位于集合视图的最左侧。
我试图用 scrollToItem 函数来做到这一点。问题是,它每次都将集合视图一直滚动到右侧。
相关代码:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return labelArray.count
}
//frees up the collection view when the scroll is active
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
isScrolling = true
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
//possible problem
if isScrolling == false {
collectionView.scrollToItem(at: indexPath, at: UICollectionViewScrollPosition.left, animated: false)
}
let myLabel = cell.viewWithTag(1) as! UILabel
let myArray = labelArray[indexPath.row]
myLabel.text = labelArray[indexPath.row]
myLabel.textColor = UIColor.white
if myArray == labelArray[1] {
cell.backgroundColor = UIColor.black
} else {
cell.backgroundColor = UIColor(red: 56/255, green: 120/255, blue: 195/255, alpha: 1)
}
return cell
}
任何帮助将不胜感激。
【问题讨论】:
标签: ios swift scroll collectionview