0
我有一個UICollectionView
有20個單元格和1節。我製作了每個單元320的寬度和高度爲304。收集單元格填充整個屏幕寬度,並且一次只能快速滾動一個單元格?
我使用scrollToItemAtIndexPath(currentIndex + 1)
使用集合視圖底部的兩個按鈕以編程方式滾動集合視圖。我只會將它們1加1。
這適用於iPhone 4s和iPhone 5/5s。使用iPhone 6/6 Plus時出現問題。
當我I scrollToItemAtIndexPath
它一次滾動2個單元格。 我該如何防止這種情況發生?我試圖讓電池適合屏幕的寬度,但只有一個電池出現,其餘的UICollectionView
是黑色的。
編輯:
下面是數據源代碼:
extension ViewController: UICollectionViewDataSource {
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 32
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("collectionCell", forIndexPath: indexPath) as! CollectionCell
cell.backgroundColor = UIColor.whiteColor()
self.current = indexPath
self.configureCell(cell, atIndexPath: indexPath) as! CollectionCell
return cell
}
func configureCell(cell: CollectionCell, atIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let string = textArray[indexPath.item % 20] as String
cell.textView.text = string
cell.textView.backgroundColor = UIColor.cloudsColor()
return cell
}
}
這裏是我怎麼滾動他們:
func buttonSelected(sender: UIButton) {
switch sender.tag {
case 0:
let previousItem: NSIndexPath = NSIndexPath(forItem: self.current.item - 1, inSection: self.current.section)
self.collectionView?.scrollToItemAtIndexPath(previousItem, atScrollPosition:UICollectionViewScrollPosition.CenteredHorizontally, animated:true)
case 1:
let nextItem: NSIndexPath = NSIndexPath(forItem: self.current.item + 1, inSection: self.current.section)
self.collectionView?.scrollToItemAtIndexPath(nextItem, atScrollPosition:UICollectionViewScrollPosition.CenteredHorizontally, animated:true)
default: break
}
}
你可以請張貼一些代碼和截圖嗎? – anhtu
我發佈了我使用的代碼。 – eeschimosu