Swift 3 Xcode 8 集合视图使用分段控件重新加载数据
Swift 3 Xcode 8 Collection View Reload Data with Segmented Control
我根据分段控件选择的索引显示来自 2 个不同数组的 2 组数据。一切正常,所有正确的数据都加载了。然而,一件非常奇怪的事情发生了:
当我按下另一个分段控件 'button' 时,集合视图应该会非常平滑地切换(在分段控件 IBAction 中实现了 reloadData())。但是在加载 'correct data' 之前它会快速闪烁。我已经尝试重新启动 Xcode,甚至将其交给朋友进行测试,但是,快速 'flash' 仍然存在。当我从分段控制 'button 2' 回到分段控制 'button1' 时也会发生这种情况。
当我仔细观察闪烁的内容时,我发现集合视图以随机顺序(或看起来如此)快速(非常快!)切换回其 'correct'订单。
这是我的视图控制器:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
var array1 = ["Red", "Yellow", "Purple"]
var array2 = ["Apple", "Banana", "Grape"]
@IBAction func segmentedControl(_ sender: UISegmentedControl) {
collectionView.reloadData()
// Reload Data
collectionView.reloadItems(at: collectionView.indexPathsForVisibleItems)
// Prevents 'flash' from .reloadData()
}
@IBOutlet weak var segmentedOutlet: UISegmentedControl!
// Creates an outlet for segmented control (that can be set)
@IBOutlet weak var collectionView: UICollectionView!
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
switch segmentedOutlet.selectedSegmentIndex {
case 0 : return array1.count
case 1 : return array2.count
default: return 0
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! collectionViewCell
switch segmentedOutlet.selectedSegmentIndex {
case 0 : cell.button.setTitle(array1[indexPath.row], for: .normal)
case 1 : cell.button.setTitle(array2[indexPath.row], for: .normal)
default : break
}
return cell
}
好的,我找到了解决这个问题的方法。基本上在:
之后
collectionView.reloadData()
添加
collectionView.reloadItems(在:collectionView.indexPathsForVisibleItems)
避免在使用集合视图切换分段控件时可能遇到的令人讨厌且相当烦人的 'flashes'。
我根据分段控件选择的索引显示来自 2 个不同数组的 2 组数据。一切正常,所有正确的数据都加载了。然而,一件非常奇怪的事情发生了:
当我按下另一个分段控件 'button' 时,集合视图应该会非常平滑地切换(在分段控件 IBAction 中实现了 reloadData())。但是在加载 'correct data' 之前它会快速闪烁。我已经尝试重新启动 Xcode,甚至将其交给朋友进行测试,但是,快速 'flash' 仍然存在。当我从分段控制 'button 2' 回到分段控制 'button1' 时也会发生这种情况。
当我仔细观察闪烁的内容时,我发现集合视图以随机顺序(或看起来如此)快速(非常快!)切换回其 'correct'订单。
这是我的视图控制器:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
var array1 = ["Red", "Yellow", "Purple"]
var array2 = ["Apple", "Banana", "Grape"]
@IBAction func segmentedControl(_ sender: UISegmentedControl) {
collectionView.reloadData()
// Reload Data
collectionView.reloadItems(at: collectionView.indexPathsForVisibleItems)
// Prevents 'flash' from .reloadData()
}
@IBOutlet weak var segmentedOutlet: UISegmentedControl!
// Creates an outlet for segmented control (that can be set)
@IBOutlet weak var collectionView: UICollectionView!
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
switch segmentedOutlet.selectedSegmentIndex {
case 0 : return array1.count
case 1 : return array2.count
default: return 0
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! collectionViewCell
switch segmentedOutlet.selectedSegmentIndex {
case 0 : cell.button.setTitle(array1[indexPath.row], for: .normal)
case 1 : cell.button.setTitle(array2[indexPath.row], for: .normal)
default : break
}
return cell
}
好的,我找到了解决这个问题的方法。基本上在:
之后collectionView.reloadData()
添加
collectionView.reloadItems(在:collectionView.indexPathsForVisibleItems)
避免在使用集合视图切换分段控件时可能遇到的令人讨厌且相当烦人的 'flashes'。