iOS 在 didSelectItemAtIndexPath 处展开 CollectionView [英] iOS unwind CollectionView at didSelectItemAtIndexPath

查看:38
本文介绍了iOS 在 didSelectItemAtIndexPath 处展开 CollectionView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我创建了一个应用程序,用户可以在其中创建自定义锻炼.您进入第一个视图添加 x 轮数,然后单击 tableview 中添加轮次的按钮.单击打开另一个带有 collectionview 的活动.

So i have created an app where users create custom workouts. You enter the first view add a x number of rounds,than you click on a button in the tableview where you add rounds.That click opens another activity with a collectionview.

所以问题就在这里,当我点击一个代表需要在回合中进行的练习的元素时,它会向我发送一个空结果.默认情况下,如果我向该字符串添加一个值,它会起作用.

So the problem is here when i click on an element witch represents the exercise that needs to be in the round it sends me an empty result.By default if i add a value to that string it works.

我注意到放松发生在我的 didSelectItemAtIndexPath 之前

I have noticed that the unwind happens before my 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 unwind(segue:UIStoryboardSegue) 放在您打算返回的 vc 中(在本例中为演示者 vc,InsertWorkout).

First, put your IBAction unwind(segue:UIStoryboardSegue) in the vc where you intend to go back (presenter vc, InsertWorkout in this case).

其次,转到情节提要并选择您将从其中展开的 vc(在本例中为集合视图),然后按住 ctrl 并从导致展开的按钮/单元格(在您的情况下为集合视图单元格)拖动到退出按钮在同一个 vc 之上,您应该会在Selection Segue"下看到一个带有 IBAction 展开方法名称的弹出窗口.

Second, go to storyboard and choose the vc where you'll unwind from (collection view in this case) and ctrl-drag from the button/cell that causes the unwind (collection view cell in your case) to the exit button on top of that same vc and you should see a pop-up with your IBAction unwind method name under 'Selection Segue'.

第三,在当前vc(本例中为collection vc)中实现prepareforsegue,将你的数据传回来.

Third, implement prepareforsegue in the current vc (collection vc in this case) to pass your data back.

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!!, 
            }
        }
    }
}

这篇关于iOS 在 didSelectItemAtIndexPath 处展开 CollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆