2015-07-04 50 views
0

我嘗試值一個表視圖控制器傳遞到另一個視圖控制器,但第二個表視圖控制器自身複製它打開兩次。你能不能幫我解決一下我貼我的代碼在這裏 這是第一個表視圖控制器代碼表視圖控制器自身複製在IOS迅速

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 



} 

}

+0

'1)'這聽起來好像您在第一個表視圖和第二個tableview之間的Storyboard中有一個segue。這將導致賽格觸發兩次;從自動SEGUE一次,然後當你調用'performSegueWithIdentifier其次:發件人:'。如果你想手動調用'performSegueWithIdentifier:發件人:'的SEGUE必須來自於第一個視圖控制器,而不是表視圖。 '2)'根據您發佈的代碼,你可以刪除你的'的tableView:didSelectRowAtIndexPath方法:'方法,只是通過賽格瑞把兩者聯繫起來的tableview。 '3)''tableView:didSelectRowAtIndexPath:'中存在錯誤 –

回答

2

聽起來好像你在第一表視圖和第二的tableview之間拖動創建了一個故事板賽格瑞。這將導致賽格觸發兩次;從自動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!財產。

相關問題