我正在使用UICollectionView
和reloadItemsAtIndexPaths
来获得最高效率并免费获得动画。
某种程度上动画有时只会触发(可能导致重用单元格?)我创建了一个简单的项目,其中包含一个UIViewController
,一个UICollectionView
和一个UIButton
来帮助我触发动画。
这是我的代码:
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
var cells = [1]
@IBOutlet weak var sampleCollectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func buttonPressed(sender: AnyObject) {
reloadVisibleSlides()
}
func reloadVisibleSlides() {
self.sampleCollectionView.reloadItemsAtIndexPaths(self.sampleCollectionView.indexPathsForVisibleItems())
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return cells.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var cell:UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! UICollectionViewCell
cell.backgroundColor = UIColor(hue: CGFloat(arc4random() % 256) / 256.0, saturation: 1, brightness: 1, alpha: 1)
return cell
}
}
如果你编译并运行它,你会发现它只会触发它。
我也试过用performBatchUpdates
我做错了吗?还有另一种方法可以达到这个目的吗?