-1
我有一個導航欄按鈕來加載從包含四個項目的底部菜單中的幻燈片。點擊其中一個菜單項將打開一個新的空白視圖。Swift - didDeselectItemAtIndexPath返回錯誤的數據
除了一件事之外,一切都按預期工作:點擊一個菜單項目前打開一個隨機視圖,從我的視圖陣列。將indexPath打印到控制檯還顯示,在每次啓動視圖後,每次返回主屏幕時都會對數組進行洗牌。我甚至不能連續兩次點擊同一個項目(當我這樣做時沒有任何反應)。
我一直在試圖弄清楚爲什麼會發生這種情況,但我真的不知道發生了什麼事情。
爲什麼我的indexPath從我的菜單項數組中返回不正確的信息?
class Setting: NSObject {
let name: SettingName
let imageName: String
init(name: SettingName, imageName: String) {
self.name = name
self.imageName = imageName
}
}
// Set list of items for menu
enum SettingName: String {
case Cancel = "Cancel"
case PostNews = "Post News"
case CheckLog = "Student Activity"
case AddStudent = "Add Student"
}
class MenuLauncher: NSObject, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
let blackView = UIView()
let collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.whiteColor()
return cv
}()
let cellId = "cellId"
let cellHeight: CGFloat = 50
let settings: [Setting] = {
let newsSetting = Setting(name: .PostNews, imageName: "menu_postnews")
let cancelSetting = Setting(name: .Cancel, imageName: "menu_cancel")
return [newsSetting, Setting(name: .CheckLog, imageName: "menu_log"),
Setting(name: .AddStudent, imageName: "menu_addstudent"), cancelSetting]
}()
var homeController: NewsController?
// Load menu
func openMenu() {
if let window = UIApplication.sharedApplication().keyWindow {
blackView.backgroundColor = UIColor(white: 0, alpha: 0.5)
blackView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleDismiss)))
window.addSubview(blackView)
window.addSubview(collectionView)
let height: CGFloat = CGFloat(settings.count) * cellHeight
let y = window.frame.height - height
collectionView.frame = CGRectMake(0, window.frame.height, window.frame.width, height)
blackView.frame = window.frame
blackView.alpha = 0
UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .CurveEaseOut, animations: {
self.blackView.alpha = 1
self.collectionView.frame = CGRectMake(0, y, self.collectionView.frame.width, self.collectionView.frame.height)
}, completion: nil)
}
}
// Dismiss menu when user touches the screen
func handleDismiss(setting: Setting) {
UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .CurveEaseOut, animations: {
self.blackView.alpha = 0
if let window = UIApplication.sharedApplication().keyWindow {
self.collectionView.frame = CGRectMake(0, window.frame.height,
self.collectionView.frame.width,
self.collectionView.frame.height)
}
}) { (completed: Bool) in
if setting.name != .Cancel {
self.homeController?.showControllerForSetting(setting)
}
}
}
// Fill in the menu with all the items
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return settings.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellId, forIndexPath: indexPath) as! MenuCell
let setting = settings[indexPath.item]
cell.setting = setting
return cell
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSizeMake(collectionView.frame.width, cellHeight)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
return 0
}
func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
let setting = self.settings[indexPath.item]
handleDismiss(setting)
print(setting.name) // Is indexPath correct?
}
override init() {
super.init()
collectionView.dataSource = self
collectionView.delegate = self
collectionView.registerClass(MenuCell.self, forCellWithReuseIdentifier: cellId)
}
}
其'didDeselectItemAtIndexPath'或'didSelectItemAtIndexPath' –
使用'didSelectItemAtIndexPath'而不是'didDeselectItemAtIndexPath',你的問題就解決了! – Lion
哦,夥計們,我一直在查看我的代碼很久,我完全錯過了!感謝您的反饋:-)愚蠢的我! –