Swift: Pass UITableViewCell label to new ViewController

后端 未结 4 1315
难免孤独
难免孤独 2020-12-01 03:23

I have a UITableView that populates Cells with data based on a JSON call. like so:

var items = [\"Loading...\"]
var indexValue = 0

// Here is SwiftyJSON co         


        
4条回答
  •  情歌与酒
    2020-12-01 03:51

    Okay..Its been 2 days I was searching for the answer that how could I be able to save the selected UITableViewCell label text data and display that data to an another label on an another View Controller which will come out after tapping on a cell. At last I have completed with the task and its successful. Here is the complete code with steps using Swift.I am using Xcode 6.4.

    Step 1.

    I have Two class assigned to the storyboard view controllers named "iOSTableViewControllerClass.swift" which is a Table View Controller and "iOSTutorialsViewControllerClass.swift" which is a normal View Controller.

    Step 2.

    Now make segue from iOSTableViewControllerClass to iOSTutorialsViewControllerClass by Control-dragging on the storyboard area and choose "show" from drop down menu. Click on this highlighted button according to the below image and perform the segue.

    Step 3.

    Now select the segue by clicking on the storyboard and give it an identifier on the Attributes Inspector. In this case I named it as "iOSTutorials"

    Step 4.

    Now on this step put a label on your cell as well as on the other view controller and make outlets of them on their corresponding classes. In my case those are "@IBOutlet weak var iOSCellLbl: UILabel!" and " @IBOutlet weak var iOSTutsClassLbl: UILabel!".

    Step 5.

    Make a string type variable on the first Table View Controller Class. I did this as "var sendSelectedData = NSString()" also Make a string type variable on the second class. I did this as "var SecondArray:String!".

    Step 6.

    Now we are ready to go. Here is the complete Code for first Class --

     // iOSTableViewControllerClass.swift
    
      import UIKit
    
      class iOSTableViewControllerClass: UITableViewController, UITableViewDataSource,UITableViewDelegate {
    
      // Creating A variable to save the text from the selected label and send it to the next view controller
    
      var sendSelectedData = NSString()
    
     //This is the outlet of the label but in my case I am using a fully customized cell so it is actually declared on a different class
    @IBOutlet weak var iOSCellLbl: UILabel!
    
    //Array for data to display on the Table View
    var iOSTableData = ["Label", "Button", "Text Field", "Slider", "Switch"];
    override func viewDidLoad() {
        super.viewDidLoad()
    
    //Setting the delegate and datasource of the table view
    
        tableView.delegate = self
        tableView.dataSource = self
    
    //Registering the class here
        tableView.registerClass(CustomTableViewCellClassiOS.self, forCellReuseIdentifier: "CellIDiOS")
    
    //If your using a custom designed Cell then use this commented line to register the nib.
        //tableView.registerNib(UINib(nibName: "CellForiOS", bundle: nil), forCellReuseIdentifier: "CellIDiOS")
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // MARK: - Table view data source
    
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // Return the number of sections.
        return 1
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // Return the number of rows in the section.
        return iOSTableData.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let CellIDentifier = "CellIDiOS"
    
     //In this case I have custom designed cells so here "CustomTableViewCellClassiOS" is the class name of the cell
        var cell:CustomTableViewCellClassiOS! = tableView.dequeueReusableCellWithIdentifier(CellIDentifier, forIndexPath: indexPath) as? CustomTableViewCellClassiOS
        if cell == nil{
            tableView.registerNib(UINib(nibName: "CellForiOS", bundle: nil), forCellReuseIdentifier: CellIDentifier)
            cell = tableView.dequeueReusableCellWithIdentifier(CellIDentifier) as? CustomTableViewCellClassiOS
    
        }
     //Here we are displaying the data to the cell label
        cell.iOSCellLbl?.text = iOSTableData[indexPath.row]
        return cell
    }
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    
        println("You selected cell #\(indexPath.row)!")
    
        // Get Cell Label text here and storing it to the variable
        let indexPathVal: NSIndexPath = tableView.indexPathForSelectedRow()!
        println("\(indexPathVal)")
        let currentCell = tableView.cellForRowAtIndexPath(indexPathVal) as! CustomTableViewCellClassiOS!;
        println("\(currentCell)")
        println("\(currentCell.iOSCellLbl?.text!)")
        //Storing the data to a string from the selected cell
        sendSelectedData = currentCell.iOSCellLbl.text!
        println(sendSelectedData)
    //Now here I am performing the segue action after cell selection to the other view controller by using the segue Identifier Name
        self.performSegueWithIdentifier("iOSTutorials", sender: self)
    }
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    
    //Here i am checking the Segue and Saving the data to an array on the next view Controller also sending it to the next view COntroller
        if segue.identifier == "iOSTutorials"{
    //Creating an object of the second View controller
            let controller = segue.destinationViewController as! iOSTutorialsViewControllerClass
    //Sending the data here
            controller.SecondArray = sendSelectedData as! String
    
     }
    

    Here is the complete code for the second Class..--

    //  iOSTutorialsViewControllerClass.swift
    
    import UIKit
    
    class iOSTutorialsViewControllerClass: UIViewController {
    
    //Creating the Outlet for the Second Label on the Second View Controller Class
    @IBOutlet weak var iOSTutsClassLbl: UILabel!
    
    //Creating an array which will get the value from the first Table View Controller Class
    var SecondArray:String!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
    //Simply giving the value of the array to the newly created label's text on the second view controller
       iOSTutsClassLbl.text = SecondArray
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    }
    

提交回复
热议问题