Swift - Segue on Dynamic TableCell

I learn quickly and am having trouble trying to run segue when I touch TableViewCell, which should pass the URL to the second view, which currently just displays it on the shortcut. I dynamically create (I saw people using programmatically, which is probably the right word), each individual cell, so in the storyboard I do not have an object to link to a different view than the view itself ... and what I did. So I connected the first view controller to the second and added code to execute the segment.

I'm not sure if this is right, my knowledge is taken from textbooks that did not quite explain what I wanted to do.

there is a code of two representations.

first view

import UIKit protocol sendInfoDelegate{ func userDidEnterInfo( WhichInfo info : String) } class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { var tableData = [] @IBOutlet weak var redditListTableView: UITableView! var selectedCellURL : String? var delegate : sendInfoDelegate? = nil override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. // change the link to change the json source getRedditJSON("http://www.reddit.com/.json") } // //Creates a connection via a task (networktask) then parses the json // func getRedditJSON(whichReddit : String){ let mySession = NSURLSession.sharedSession() let url: NSURL = NSURL(string: whichReddit) let networkTask = mySession.dataTaskWithURL(url, completionHandler : {data, response, error -> Void in var err: NSError? var theJSON = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSMutableDictionary let results : NSArray = theJSON["data"]!["children"] as NSArray dispatch_async(dispatch_get_main_queue(), { self.tableData = results self.redditListTableView.reloadData() }) }) networkTask.resume() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } //needs to be implemented func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return tableData.count } //creates the whole table func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell") let redditEntry : NSMutableDictionary = self.tableData[indexPath.row] as NSMutableDictionary cell.textLabel?.text = redditEntry["data"]!["title"] as? String cell.detailTextLabel?.text = redditEntry["data"]!["author"] as? String return cell } // action to be taken when a cell is selected func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { let redditEntry : NSMutableDictionary = self.tableData[indexPath.row] as NSMutableDictionary self.selectedCellURL = redditEntry["data"]!["url"] as? String self.performSegueWithIdentifier("passInfo" , sender: indexPath) println(self.selectedCellURL!) if delegate != nil { let information:String = self.selectedCellURL! println("ciao") delegate?.userDidEnterInfo(WhichInfo: information) self.navigationController?.popViewControllerAnimated(true) } 

second view

 import UIKit class WebPageController : UIViewController, sendInfoDelegate { var infoFromSVC: String? @IBOutlet weak var labelVC: UILabel! func userDidEnterInfo(WhichInfo info: String) { self.infoFromSVC = info labelVC.text = infoFromSVC } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "passInfo"{ let firstVController : ViewController = segue.destinationViewController as ViewController firstVController.delegate = self } } } 

Thanks.

+5
source share
1 answer

To transfer any data to the second view controller, you need to implement the prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) method prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) In your first view controller and transfer the data to your second view controller through the segue.destinationViewController object.

eg

 // this method must be in first view controller override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "passInfo" { var secondViewController : SecondViewController = segue.destinationViewController as SecondViewController var indexPath = self.tableview.indexPathForSelectedRow() //get index of data for selected row secondViewController.data = self.dataArray.objectAtIndex(indexPath.row) // get data by index and pass it to second view controller } } 

Code for retrieving data in a second view controller

 override func viewDidLoad() { super.viewDidLoad() self.label.text = self.data } 

A data variable should be defined as a property of your second view controller.

+2
source

Source: https://habr.com/ru/post/1204434/


All Articles