3

I successfully set up my tableview so that it can correctly pass a specific String depending on the row that was tapped. However, I don't know how to retrieve this data. I know how to do it in java but I am new to swift and I find it confusing.

Sender ControlView:

import UIKit

class TechniqueListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    let cellContent = ["Stance", "Move Forward", "Move Backward", "Move Right", "Move Left", "Jab", "Cross", "Hook", "Uppercut", "Body Jab", "Body Cross", "Body Hook", "Body Uppercut", "Leg Kick", "Body Kick", "Switching Stances", "Switch Leg Kick", "Switch Body Kick", "Push Kick", "Switch Push Kick", "Front Push Kick", "Switch Front Push Kick", "Spinning Back Kick", "Knee", "Switch Knee", "Elbow", "Tornado Kick"]

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        //gets # of row
        return cellContent.count
    }

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        //defines content of each cell

        let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "TechniqueCell")
        cell.textLabel?.text = cellContent[indexPath.row]

        return cell

    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cellIndex = indexPath.row
        if (cellIndex == 0){
            performSegue(withIdentifier: value(forKey: "Stance") as! String, sender: IndividualTechniqueController())
        }
        else if cellIndex == 1{
            performSegue(withIdentifier: "Move Forward", sender: IndividualTechniqueController())
        }
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

Retrieving ControlView:

    //I want an if else statement here
    if senderString == "Stance"{  //  <---- correct me if this is wrong
    }
    else if senderString == "Move Forward"
    {

}
Bista
  • 7,869
  • 3
  • 27
  • 55
ESPA_NETwork
  • 165
  • 2
  • 14
  • which are the string in cellContent array and also what you exactly want to do in didSelectedRowAt indexPath method ? – Yagnesh Dobariya Sep 08 '16 at 05:14
  • What is `IndividualTechniqueController` is it current controller? Also you just want to pass string object from `cellContent` array? Also show the storyboard segue with its identifier that you have created. – Nirav D Sep 08 '16 at 05:15
  • at index 0-1: cellContent = ["Stance", "Move Forward"] I just want to put use those strings in another control view as a switch statement so I know which UI components to use for the second control view – ESPA_NETwork Sep 08 '16 at 05:18
  • I will edit my question – ESPA_NETwork Sep 08 '16 at 05:18
  • @GrantEspanet Your destinationController is same for both the cell? – Nirav D Sep 08 '16 at 05:20
  • Yes NDoc, I updated – ESPA_NETwork Sep 08 '16 at 05:24
  • I think you need this: http://stackoverflow.com/questions/26089152/sending-data-with-segue-with-swift – Lyuzonggui Sep 08 '16 at 05:28
  • @NDoc I don't have code for that. I wasn't aware I needed that – ESPA_NETwork Sep 08 '16 at 05:30
  • @GrantEspanet Have you added segue in storyboard or not? Or you don't know what is segue? – Nirav D Sep 08 '16 at 06:19
  • @NDoc, I have a small idea of what segue is. Do I need it to accomplish my task? – ESPA_NETwork Sep 08 '16 at 06:25
  • @GrantEspanet Check this link https://developer.apple.com/library/ios/recipes/xcode_help-IB_storyboard/Chapters/StoryboardSegue.html In this link segue is created between cell and viewcontroller you need to create segue between two controllers that is from `TechniqueListViewController` to `IndividualTechniqueController` after that set segue identifier. do this and inform here and show the screen shot also after done that, so i can check it is done properly or not. – Nirav D Sep 08 '16 at 06:33
  • @NDoc, I noticed segues drag for button,s navigation bar items, etc. But for table views it doesn't work – ESPA_NETwork Sep 08 '16 at 06:39
  • You can create segue for tableViewCell not for tableview, Also you need to create segue from oneVC to SecondVC, so select your viewController then press Ctrl and drag it to the destinationVC – Nirav D Sep 08 '16 at 06:40
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122883/discussion-between-grant-espanet-and-ndoc). – ESPA_NETwork Sep 08 '16 at 07:02

1 Answers1

1

Hi take one variable of data type String(your required data type) in TechniqueListViewController as like follow

var previouspageData: String

use that variableble in tableview didselect method to send data to that controller

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

self.previouspageData = "your data"
 let cellIndex = indexPath.row
if (cellIndex == 0){
    performSegue(withIdentifier: value(forKey: "Stance") as! String, sender:self)
}
else if cellIndex == 1{
     performSegue(withIdentifier: "Move Forward", sender:self)
}
}

use that data as follow inyour destination controoler

 override func performSegueWithIdentifier(identifier: String, sender: AnyObject?) {
if sender is TechniqueListViewController {
 if  (sender as! TechniqueListViewController).previouspageData == "Stance"{ 
 }
 else if (sender as! TechniqueListViewController).previouspageData == "Move Forward"
 {

  }
}
}
Satyanarayana
  • 1,059
  • 6
  • 16