Ios Swift-返回错误数据的DidItemAtIndexPath

Ios Swift-返回错误数据的DidItemAtIndexPath,ios,swift,Ios,Swift,我有一个导航栏按钮,从底部加载包含四个项目的滑入菜单。单击其中一个菜单项将打开一个新的空视图 除了一件事之外,一切都正常工作:单击菜单项当前会从我的视图数组中打开一个随机视图。将indexPath打印到控制台还显示,每次启动视图后返回主屏幕时,数组都会被洗牌。我甚至不能连续两次单击同一项(这样做时不会发生任何事情) 我一直在想为什么会这样,但我真的不知道发生了什么 为什么我的indexPath从菜单项数组返回不正确的信息? class Setting: NSObject { let name:

我有一个导航栏按钮,从底部加载包含四个项目的滑入菜单。单击其中一个菜单项将打开一个新的空视图

除了一件事之外,一切都正常工作:单击菜单项当前会从我的视图数组中打开一个随机视图。将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)
}
}

正如Ketan Parmar和Anbu Karthik提到的:我选择了不正确的函数参数。我不知怎么写了didDeselectItemAtIndexPath而不是didSelectItemAtIndexPath。只是提醒您在实现新功能时要仔细检查代码。谢谢大家

我的代码

func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {

let setting = self.settings[indexPath.item]
handleDismiss(setting)
print(setting.name) // Is indexPath correct?
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

let setting = self.settings[indexPath.item]
handleDismiss(setting)
print(setting.name) // Is indexPath correct?
}

正确的代码

func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {

let setting = self.settings[indexPath.item]
handleDismiss(setting)
print(setting.name) // Is indexPath correct?
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

let setting = self.settings[indexPath.item]
handleDismiss(setting)
print(setting.name) // Is indexPath correct?

}

its
DidDecelectItemAtIndexPath
didSelectItemAtIndexPath
使用
didSelectItemAtIndexPath
而不是
DidDecelectItemAtIndexPath
,您的问题将得到解决!哦,伙计们,我一直在看我的代码很长时间,我完全错过了!谢谢你的反馈:-)愚蠢的我!