问题
I need to use didSelectRowAtIndexPath
to pass
my current Cell after Searching (this is the only way that I found),
but how can I pass this cell name to the SecondViewController?
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if(tableView == self.searchDisplayController!.searchResultsTableView){
if let cell: UITableViewCell = tableView.cellForRowAtIndexPath(indexPath){
if let cellTextLabel: UILabel = cell.textLabel{
let cellSelected = cellTextLabel.text!
}
}
}
In the SecondViewController, I need to use the TextView to present the current text to the selected cell.
回答1:
Try this code :
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if(tableView == self.searchDisplayController!.searchResultsTableView){
if let cell: UITableViewCell = tableView.cellForRowAtIndexPath(indexPath){
if let cellTextLabel: UILabel = cell.textLabel{
let cellSelected = cellTextLabel.text!
// pass your segue name
self.performSegueWithIdentifier("segueToDetailVC", sender: cellTextLabel.text!)
}
}
}
pass data
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "segueToDetailVC")
{
var str: String = sender as! String
// your ViewController class object
var detail: DetailVC = segue.destinationViewController as! DetailVC
// pass text in String Variable
detail.text = str
}
}
回答2:
Add an attribute secondViewController in the destination view controller, and use prepareForSegue
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "segueTest") {
var svc = segue!.destinationViewController as secondViewController;
svc.toPass = cellTextLabel.text
}
}
in secondViewController you can define one variable like String
var toPass:String!
In the secondViewController under the viewDidLoad function add this code
println(\(toPass))
来源:https://stackoverflow.com/questions/31852554/how-to-passing-date-between-two-storyboard-with-didselectrowatindexpathin-swift