2016-08-23 49 views
-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) 
} 
} 
+3

其'didDeselectItemAtIndexPath'或'didSelectItemAtIndexPath' –

+0

使用'didSelectItemAtIndexPath'而不是'didDeselectItemAtIndexPath',你的问题就解决了! – Lion

+0

哦,伙计们,我一直在查看我的代码很久,我完全错过了!感谢您的反馈:-)愚蠢的我! –

回答

0

由于科坦帕尔马,并提到庵埠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? 

}