我试图创建一个自动滚动的无限滚动UICollectionView。当在collectionView中点击一个单元格时,我需要删除该单元格。
我有一些工作,但问题是,每次它到达collectionView的末尾,当它应该循环'结束了,它结结巴巴并跳了起来。我知道这是因为我使用collectionView.reloadData()
。我的问题是,有没有其他方法可以在不调用reloadData
的情况下执行此操作,因此它不会跳转?
(此处' s a video of the problem)
这是我的代码:
class CollectionViewController: UICollectionViewController {
var animationTimer : NSTimer!
var data = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p"]
override func viewDidLoad() {
super.viewDidLoad()
startTimer()
}
func startTimer() {
animationTimer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: "onFire:", userInfo: nil, repeats: true)
}
func onFire(sender : AnyObject) {
collectionView?.contentOffset = CGPointMake((collectionView?.contentOffset.x)! + 1, 0)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func scrollViewDidScroll(scrollView: UIScrollView) {
let xContentOffset = scrollView.contentOffset.x
let contentHeight = scrollView.contentSize.width - (collectionView?.frame.width)!
if xContentOffset >= contentHeight {
collectionView?.reloadData()
collectionView?.contentOffset = CGPointMake(0, 0)
}
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return data.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CollectionViewCell
cell.label.text = data[indexPath.row]
return cell
}
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
data.removeAtIndex(indexPath.row)
collectionView.deleteItemsAtIndexPaths([indexPath])
}
谢谢!