iOS在didSelectItemAtIndexPath

时间:2015-10-13 19:05:54

标签: ios swift uitableview uicollectionview

所以我创建了一个用户创建自定义锻炼的应用程序。 您输入第一个视图添加x个轮次,而不是单击桌面视图中添加圆形的按钮。单击此按钮将打开另一个带有集合视图的活动。

所以问题就在这里,当我点击一个元素时,女巫代表需要在该轮次中的练习,它向我发送一个空结果。默认情况下,如果我为该字符串添加一个值,它就可以工作。

我注意到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)
    }

1 个答案:

答案 0 :(得分:0)

以下是放卷的工作原理:

首先,将您的IBAction展开(segue:UIStoryboardSegue)放在您打算返回的vc中(演示者vc,在这种情况下为InsertWorkout)。

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

第三,在当前的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!!, 
            }
        }
    }
}