【发布时间】:2015-07-04 15:46:51
【问题描述】:
我尝试将一个表视图控制器的值传递给另一个视图控制器,但第二个表视图控制器复制自身,它打开了两次。你能帮我解决一下吗?我把我的代码贴在这里 这是第一个表格视图控制器代码
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
}
}
这是第二个表格视图控制器代码。
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
}
}
【问题讨论】:
-
1)听起来好像您的情节提要中的第一个表视图和第二个表视图之间有一个segue。这将导致 segue 触发两次;一次来自自动转场,然后在您致电performSegueWithIdentifier:sender:时。如果你想手动调用performSegueWithIdentifier:sender:,segue 必须来自第一个视图控制器而不是表视图。2)根据您发布的代码,您可以删除您的tableView:didSelectRowAtIndexPath:方法并通过 segue 链接两个 tableview。3)tableView:didSelectRowAtIndexPath:中存在错误
标签: ios swift uitableview