iOS在didSelectItemAtIndexPath上展开CollectionView

所以我创build了一个应用程序,用户创build自定义的锻炼。 你input第一个视图添加轮的数量,比你点击一个button,在你添加的roundviews.That点击一个button打开另一个活动与collectionview。

所以问题是在这里,当我点击一个元素女巫代表的运动,需要在这一轮它给我一个空的结果。默认情况下,如果我添加一个值,该string的作品。

我已经注意到unwind发生在我的didSelectItemAtIndexPath之前

import UIKit class InsertWorkout: UIViewController{ @IBOutlet var InsertRoundTable: UITableView! @IBOutlet weak var txtName: UITextField! var RoundNumber : NSMutableArray = ["Round 1"] var RoundLabel : NSMutableArray = [""] var RoundExercise : NSMutableArray = ["add-1"] var RoundExerciseImages : NSMutableArray = ["providno"] var RoundNumber_Count=1 var RoundNumber_Add_Counter=1 var studentData : StudentInfo! override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } //MARK: UIButton Action methods @IBAction func btnBackClicked(sender: AnyObject) { self.navigationController?.popViewControllerAnimated(true) } @IBAction func btnSaveClicked(sender: AnyObject) { if(txtName.text == "") { Util.invokeAlertMethod("", strBody: "Please enter workout name.", delegate: nil) } else { let studentInfo: StudentInfo = StudentInfo() studentInfo.workout_name = txtName.text! studentInfo.workout_benefit_1=" CUSTOM " studentInfo.workout_benefit_2=" WORKOUT " studentInfo.workout_benefit_3="" studentInfo.workout_requiremnets="arsutech.com" studentInfo.workout_time="4min" let isInserted = ModelManager.getInstance().addWorkoutData(studentInfo) if isInserted { Util.invokeAlertMethod("", strBody: "Workout added", delegate: nil) } else { Util.invokeAlertMethod("", strBody: "Error while adding workout.", delegate: nil) } self.navigationController?.popViewControllerAnimated(true) } } override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { self.view.endEditing(true) InsertRoundTable.endEditing(true) } //UITableView func numberOfSectionsInTableView(tableView: UITableView) -> Int { // #warning Potentially incomplete method implementation. // Return the number of sections. return 1 } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // #warning Incomplete method implementation. // Return the number of rows in the section. return RoundNumber.count } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell : InsertRoundCell! = tableView.dequeueReusableCellWithIdentifier("InsertRoundCell") as! InsertRoundCell if(cell == nil) { cell = NSBundle.mainBundle().loadNibNamed("InsertRoundCell", owner: self, options: nil)[0] as! InsertRoundCell; } let exerviseName = RoundNumber[indexPath.row] let exerciseLabels = RoundLabel[indexPath.row] let exerciseImage = RoundExercise[indexPath.row] let exerciseImage_Holder = RoundExerciseImages[indexPath.row] cell.insert_label_Round.text = exerviseName as? String cell.addWorkout_Label.text = exerciseLabels as? String cell.addExercise_Holder?.image = UIImage(named: exerciseImage_Holder as! String) as UIImage? cell.addWorkoutBut.setBackgroundImage(UIImage(named: exerciseImage as! String) as UIImage?, forState: UIControlState.Normal) cell.addWorkoutBut.tag = indexPath.row cell.addWorkoutBut.addTarget(self, action: "logAction:", forControlEvents: .TouchUpInside) cell.selectionStyle = UITableViewCellSelectionStyle.None return cell as InsertRoundCell } @IBAction func logAction(sender: UIButton){ //self.RoundNumber.replaceObjectAtIndex(sender.tag, withObject: "Ezel") //let titleString = self.RoundNumber[sender.tag] as? String //Util.invokeAlertMethod("", strBody: titleString!, delegate: nil) self.InsertRoundTable.reloadData() } override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) { let destinationVC = segue.destinationViewController as! ChoseExercise destinationVC.id=sender.tag RoundNumber_Count=sender.tag } @IBAction func unwinndChoseExercise(segue:UIStoryboardSegue){ if let svc = segue.sourceViewController as? ChoseExercise{ self.RoundLabel.replaceObjectAtIndex(RoundNumber_Count, withObject: "\(svc.exercise_label)") self.RoundExercise.replaceObjectAtIndex(RoundNumber_Count, withObject: "providno") self.RoundExerciseImages.replaceObjectAtIndex(RoundNumber_Count, withObject: "\(svc.exercise_image)") self.InsertRoundTable.reloadData() } } //Add Round @IBAction func addRound(sender: AnyObject) { if(RoundNumber_Add_Counter<12){ RoundNumber_Add_Counter++ self.RoundNumber.addObject("Round \(RoundNumber_Add_Counter)") self.RoundLabel.addObject("") self.RoundExercise.addObject("add-1") self.RoundExerciseImages.addObject("providno") self.InsertRoundTable.reloadData() }else{ Util.invokeAlertMethod("", strBody: "This is the maximum number of rounds", delegate: nil) } } } override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return self.exercisesNames.count } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cellexercises_insert", forIndexPath: indexPath) as! Insert_ExerciseCollectionViewCell cell.insert_exerciseImage?.image = self.exercisesImages[indexPath.row] cell.insert_exerciseLabel?.text = self.exercisesNames[indexPath.row] cell.insert_exercisesHardnessImg?.image = self.exercisesHardnessImg[indexPath.row] return cell } func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { exercise_label="\(exercisesNames[indexPath.row])" exercise_image="\(exercises_Exervise_Row_Names[indexPath.row])" dismissViewControllerAnimated(true, completion: nil) } 

这是如何展开工作:

首先,把你的IBAction放到你打算返回的vc中(在这种情况下,vc,InsertWorkout),放开(segue:UIStoryboardSegue)。

其次,去故事板,并select从哪里放松(在这种情况下的集合视图)的VC,并从button/单元格,导致展开(集合视图单元格在您的情况下)拖动到退出button顶部相同的VC,你应该看到一个popup与您的IBAction展开方法名称下selectSegue。

第三,在当前的vc(在这种情况下收集vc)执行prepareforsegue来传回你的数据。

 class InsertWorkout { IBAction func unwind(segue:UIStoryboardSegue) { //use the data passed from prepare to update your ui or you can also communicate to the sender using segue.sourceVC } } class CollectionVC { override func prepareForSegue(segue: UIStoryboadSegue, sender: AnyObject?){ if segue.identifier == "whatever you put in storyboard for segue id" { if let inserWorkoutVC = segue.destinationViewController as? InsertWorkout { //pass the data here but don't attempt to update your destination's view ui!!, } } } }