2016-03-04 86 views
0

有一個將數據傳遞給ViewController的UITableViewCell。現在它崩潰了,我不知道爲什麼。在我開始工作之前,我每次點擊其中一個單元格時都會出現fata錯誤。Swift:將UITableViewCell標籤傳遞給新的ViewController **破壞**

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 
    @IBOutlet weak var signin: UITextField! 
    @IBOutlet weak var password: UITextField! 


    private let datetimes = ["02/25/16 5:47 PM", "02/25/16 2:47 PM", "02/21/16 5:33 AM"] 
    private let user = ["StevieE11", "Sikes911", "MaggieMae"] 
    private let feedback = ["The food was fucking terrible!", "Best food this side of the mason dixon line!", "If that waiter looks at me again I'm going to bite the shit out of him"] 

    var sendSelectedData = NSString() 

    override func viewWillAppear(animated: Bool) { 
     navigationItem.title = "Inbox" 
     //navigationController!.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Helvetica Neue", size: 24)!] 

    } 

    func textFieldShouldReturn(textField: UITextField!) -> Bool { 
     textField.resignFirstResponder() 
     return true 
    } 


    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return user.count 
    } 



    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

     // Create a new cell with the reuse identifier of our prototype cell 
     // as our custom table cell class 
     let cell = tableView.dequeueReusableCellWithIdentifier("myProtoCell") as! MyTableView 
     // Set the first row text label to the firstRowLabel data in our current array item 
     cell.user.text = user[indexPath.row] 
     // Set the second row text label to the secondRowLabel data in our current array item 
     cell.feedback.text = feedback[indexPath.row] 
     //cell.feedback.lineBreakMode = NSLineBreakMode.ByWordWrapping 
     //cell.feedback.numberOfLines = 2 
     // Set the datetime label to the datetime array 
     cell.dateTime.text = datetimes[indexPath.row] 
     // Return our new cell for display 
     return cell 
    } 

    func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { 

    } 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

     //println("You selected cell #\(indexPath.row)!") 

     // Get Cell Label text here and storing it to the variable 
     let indexPathVal: NSIndexPath = tableView.indexPathForSelectedRow! 
     //println("\(indexPathVal)") 
     let currentCell = tableView.cellForRowAtIndexPath(indexPathVal) as! MyTableView!; 
     //println("\(currentCell)") 
     //println("\(currentCell.iOSCellLbl?.text!)") 
     //Storing the data to a string from the selected cell 
     currentCell.user.text! = user[indexPath.row] 
     sendSelectedData = currentCell.user.text! 
     print(sendSelectedData) 
     //Now here I am performing the segue action after cell selection to the other view controller by using the segue Identifier Name 
     self.performSegueWithIdentifier("ShowFeedbackSegue", sender: self) 
    } 

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
     //Here i am checking the Segue and Saving the data to an array on the next view Controller also sending it to the next view COntroller 
     if segue.identifier == "ShowFeedbackSegue"{ 
      //Creating an object of the second View controller 
      let controller = segue.destinationViewController as! FeedbackViewController 
      //Sending the data here 
      controller.SecondArray = sendSelectedData as String! 
      } 
     } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
} 

有什麼想法?

感謝

+0

u能猜出它的確切位置崩潰行了? – Tj3n

+0

當我點擊單元格以繼續到下一個視圖控制器時它崩潰 – StevieE11

回答

0
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
if let identifier = segue.identifier { 
    switch identifier{ 
    case "ShowFeedbackSegue": 
     if let controller = segue.destinationViewcontroller as? FeedbackViewController { 
      controller.SecondArray = sendSelectedData as String! // SecondArray must be of //type string 
     } 
    default: break 
    } 
} 

}

相關問題