Reputation: 556
I try to pass value one table view controller to another view controller but second table view controller duplicate itself it opens twice. Could you help me fix about it i pasted my code here Here is the first table view controller codes
var dataPassed:String!
var cars = [String]()
var valueToPass:String!
var passedValue:String!
override func viewDidLoad() {
super.viewDidLoad()
cars = [dataPassed,"Audi","Volkswagen"]
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return cars.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("carCell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel!.text = cars[indexPath.row]
return cell
}
override func tableView(tableView: (UITableView!), didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Get Cell Label
let indexPath = tableView.indexPathForSelectedRow();
let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!;
valueToPass = currentCell.textLabel!.text
performSegueWithIdentifier("secondide", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "secondide") {
// initialize new view controller and cast it as your view controller
var viewController = segue.destinationViewController as! SecondTableViewController
// your new view controller should have property that will store passed value
viewController.value123 = valueToPass
}
}
Here is the second table view controller codes.
var SecondArray=[String]()
var value123:String!
override func viewDidLoad() {
super.viewDidLoad()
SecondArray = ["Kemal","Ekren","Hello"]
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return SecondArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("SecondCell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel!.text = SecondArray[indexPath.row]
return cell
}
}
Upvotes: 0
Views: 910
Reputation: 5891
It sounds as if you have a segue in your Storyboard created by dragging between the first table view and the second tableview. This will cause the segue to trigger twice; once from the automatic segue and then secondly when you call performSegueWithIdentifier:sender:
.
The way I would write this is to delete yourtableView:didSelectRowAtIndexPath:
method then alter your prepareForSegue:sender:
method to be something similar to this:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject? {
// Find the correct segue
if (segue.identifier == "secondide") {
// initialize new view controller and cast it as your view controller
var viewController = segue.destinationViewController as! SecondTableViewController
// Find the index path of the selected row
let selectedIndex = self.tableView.indexPathForCell(sender as UITableViewCell)
// your new view controller should have property that will store passed value
viewController.value123 = cars[selectedIndex]
}
}
Then ensure the segue is created by control-dragging from the first table view to the second view controller. This will trigger the segue as soon as someone taps a cell.
You will also no longer need your var valueToPass:String!
property.
Upvotes: 2