【问题标题】:Table view Controller duplicate itself in IOS swift表视图控制器在 IOS swift 中复制自身
【发布时间】: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


【解决方案1】:

听起来好像您的故事板中有一个通过在第一个表视图和第二个表视图之间拖动而创建的 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。

您也将不再需要您的 var valueToPass:String! 属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-10
    • 1970-01-01
    • 2018-11-09
    • 1970-01-01
    • 2020-01-23
    • 2017-06-10
    相关资源
    最近更新 更多