表视图控制器在IOS swift中复制自身 [英] Table view Controller duplicate itself in IOS swift

查看:84
本文介绍了表视图控制器在IOS swift中复制自身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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



}

}

推荐答案

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

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:.

我写这个的方式是删除 tableView:didSelectRowAtIndexPath:方法,然后将 prepareForSegue: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]
    }
}

然后确保segue是由控制创建的 - 从第一个表视图拖动到第二个视图控制器。一旦有人点击一个单元格,这将触发segue。

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.

您还将不再需要 var valueToPass:String! property。

You will also no longer need your var valueToPass:String! property.

这篇关于表视图控制器在IOS swift中复制自身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆