Swift - didDeselectItemAtIndexPath返回错误的数据

问题描述 投票:-1回答:1

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

除了一件事之外,一切都按预期工作:单击菜单项当前会从我的视图数组中打开一个随机视图。将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)
}
}
ios swift
1个回答
1
投票

正如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?

}

© www.soinside.com 2019 - 2024. All rights reserved.