表格视图控制器在IOS swift中复制

时间:2015-07-04 15:46:51

标签: ios swift uitableview

我尝试将值一个表视图控制器传递给另一个视图控制器,但第二个表视图控制器复制它自己打开两次。你能帮我解决一下吗?我在这里粘贴了我的代码 这是第一个表视图控制器代码

    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 个答案:

答案 0 :(得分:2)

听起来好像你的故事板中有一个segue是通过在第一个表视图和第二个表视图之间拖动而创建的。这将导致segue触发两次;一次来自自动segue,然后再来一次performSegueWithIdentifier:sender:

我写这个的方法是删除你的tableView:didSelectRowAtIndexPath:方法然后改变你的prepareForSegue:sender:方法,使其与此类似:

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]
    }
}

然后确保通过控制从第一个表视图拖动到第二个视图控制器来创建segue。一旦有人敲击一个单元格,这将触发segue。

您也不再需要var valueToPass:String!财产。