3
UICollectionView
在重新加載部分時不會從視圖層次結構中刪除可重用視圖。這會造成UIAutomation
的問題。UICollectionView reloadSection不會從視圖層次結構中刪除舊視圖
我創建了一個簡單的應用程序,它使用UICollectionView
並顯示頁眉,頁腳和單元格。在點擊「Reload
」時,collectionView
顯示不同數量的單元格。請參考下面的圖片。
在啓動應用程序,它顯示了2個細胞。點擊「Reload
」第一次顯示8 cells
和點擊「Reload
」第二次顯示18 cells
。點擊「重新加載」第三次顯示2個單元格。現在,在這一刻,如果我們使用Xcode View debugger
調試視圖,那麼在禁用「僅顯示顯示的視圖」時,它會在視圖層次結構中顯示多個單元格。請參考以下圖片:
這也是UIAutomation
在獲得元素創造的問題。我不確定我的實施有什麼問題。
下面是我的控制器代碼:
struct CellData {
var title: String
var value: String
}
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
@IBOutlet var collectionView: UICollectionView!
private var data: [CellData]!
private var data1: [CellData]!
private var data2: [CellData]!
private var data3: [CellData]!
private var reloadIndex = 1
private var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.data1 = [
CellData(title: "Cell Title 1", value: "Value 1"),
CellData(title: "Cell Title 2", value: "Value 2")]
self.data = self.data1
self.data2 = [
CellData(title: "Cell Title 1", value: "Value 1"),
CellData(title: "Cell Title 2", value: "Value 2"),
CellData(title: "Cell Title 3", value: "Value 3"),
CellData(title: "Cell Title 4", value: "Value 4"),
CellData(title: "Cell Title 5", value: "Value 5"),
CellData(title: "Cell Title 6", value: "Value 6"),
CellData(title: "Cell Title 7", value: "Value 7"),
CellData(title: "Cell Title 8", value: "Value 8")]
self.data3 = [
CellData(title: "Cell Title 1", value: "Value 1"),
CellData(title: "Cell Title 2", value: "Value 2"),
CellData(title: "Cell Title 3", value: "Value 3"),
CellData(title: "Cell Title 4", value: "Value 4"),
CellData(title: "Cell Title 5", value: "Value 5"),
CellData(title: "Cell Title 6", value: "Value 6"),
CellData(title: "Cell Title 7", value: "Value 7"),
CellData(title: "Cell Title 8", value: "Value 8"),
CellData(title: "Cell Title 9", value: "Value 9"),
CellData(title: "Cell Title 10", value: "Value 10"),
CellData(title: "Cell Title 11", value: "Value 11"),
CellData(title: "Cell Title 12", value: "Value 12"),
CellData(title: "Cell Title 13", value: "Value 13"),
CellData(title: "Cell Title 14", value: "Value 14"),
CellData(title: "Cell Title 15", value: "Value 15"),
CellData(title: "Cell Title 16", value: "Value 16"),
CellData(title: "Cell Title 17", value: "Value 17"),
CellData(title: "Cell Title 18", value: "Value 18")]
self.collectionView.registerNib(UINib(nibName: "CollectionCell", bundle: nil), forCellWithReuseIdentifier: "CollectionCell")
self.collectionView.registerNib(UINib(nibName: "SectionHeader", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "SectionHeader")
self.collectionView.registerNib(UINib(nibName: "SectionFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "SectionFooter")
}
@IBAction func reload() {
self.activityIndicator.center = self.view.center
self.view.addSubview(self.activityIndicator)
self.activityIndicator.startAnimating()
self.view.userInteractionEnabled = false
let waitTime = 2 * Double(NSEC_PER_SEC)
let dispatchTime = dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW), Int64(waitTime))
dispatch_after(dispatchTime, dispatch_get_main_queue()) {
self.reloadIndex += 1
if self.reloadIndex > 3 {
self.reloadIndex = 1
}
switch self.reloadIndex {
case 2:
self.data = self.data2
break
case 3:
self.data = self.data3
break
default:
self.data = self.data1
}
self.collectionView.reloadSections(NSIndexSet(index: 0))
//self.collectionView.reloadData()
self.activityIndicator.stopAnimating()
self.activityIndicator.removeFromSuperview()
self.view.userInteractionEnabled = true
}
}
@IBAction func print() {
printViewHierarchyInJSON(self.view)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.data.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let compatibleCell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionCell", forIndexPath: indexPath)
(compatibleCell as? CollectionCell)?.setData(self.data[indexPath.item])
return compatibleCell
}
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
var supplementaryView = UICollectionReusableView()
switch kind {
case UICollectionElementKindSectionHeader:
supplementaryView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "SectionHeader", forIndexPath: indexPath)
case UICollectionElementKindSectionFooter:
supplementaryView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionFooter, withReuseIdentifier: "SectionFooter", forIndexPath: indexPath)
default:
break
}
return supplementaryView
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSizeMake(collectionView.bounds.size.width, 50)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSizeMake(collectionView.bounds.size.width, 104)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
return CGSizeMake(collectionView.bounds.size.width, 86)
}
}
我已經上傳此示例應用程序上dropbox。
預先感謝您。
後UICollectionView「cellForItemAt」代碼 –
我加入了Dropbox的鏈接項目。它是:https://www.dropbox.com/s/gp20x3zz15f0vqx/CollectionViewDemo.zip?dl=0 – kkumpavat