出於某種原因,我的collectionViewCell在選擇時無法識別。直到另一個單元格被觸摸後才能識別出前一個單元格。爲了解釋我是如何實現這一點的,我將下面的代碼添加到我的collectionView的didDeselectItemAtIndexPath
方法中:println("user tapped on cell # \(indexPath.row)")
。當我運行應用程序並選擇一個單元格時,我的控制檯不會響應,直到我點擊另一個單元格,然後它讀取我添加的println
。例如,如果我選擇第一個單元格,調試器將不會打印任何內容,直到我選擇另一個單元格,然後控制檯讀取「user tapped on thumbnail # 0
」。爲什麼我的collectionViewCell顯示異常行爲?
現在我已經爲我的collectionView添加了一個動畫,放大了選中的每個單元格,所以這就是我知道它不是indexPath問題的原因,因爲在控制檯中打印的單元格indexPath#是正確的單元格在視圖中被放大了,但就像我說的那樣,單元在它的選擇上沒有動畫,直到我選擇另一個單元之後。
這是我的CollectionView委託和數據源的邏輯:
// MARK: UICollectionViewDataSource
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.books.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! MyCollectionViewCell
// Configure the cell
let book = self.books[indexPath.row]
let coverImage = book.coverImage
if coverImage == nil {
book.fetchCoverImage({ (image, error) -> Void in
if self.collectionView != nil {
collectionView.reloadItemsAtIndexPaths([indexPath])
}
})
} else {
let imageView = cell.imageView
imageView.image = book.coverImage
}
return cell
}
override func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
let cell = collectionView.cellForItemAtIndexPath(indexPath) as! MyCollectionViewCell
let book = self.books[indexPath.row]
self.selectedImageView = cell.imageView
if !isModeModal {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier("DetailViewController") as! DetailViewController
controller.imageSelected = book.coverImage
self.navigationController?.pushViewController(controller, animated: true)
}
println("user tapped on thumbnail # \(indexPath.row)")
}
}
爲什麼這種行爲發生?
看做** **德selectItemAtIndexPath' - 你想'didSelectItemAtIndexPath' – Paulw11
哦,天哪,這是尷尬哈哈@ Paulw11how可能錯過了 –
自動完成的風險... – Paulw11