Swift:将UITableViewCell标签传递给新的ViewController ** CRASHING **

时间:2016-03-04 02:17:46

标签: ios crash swift2 segue uitableview

拥有一个将数据传递给ViewController的UITableViewCell。现在它崩溃了,我不知道为什么。在我之前工作时,每次点击其中一个单元格时,我就开始出现fata错误。

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

有什么想法吗?

由于

1 个答案:

答案 0 :(得分: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
    }
}

}