使用tableViewController中的数据更新secondViewController上的标签

时间:2015-12-12 12:13:06

标签: ios uitableview back-button reloaddata

我有一个tableView作为初始控制器,在secondViewController中有几个标签。 当我用我想要的数据创建一个单元格时,想法是在secondViewController标签中显示该数据。一切正常,但是,当我点击后退按钮时,第二个VC中的标签会更新,返回到表视图并再次选择行。 如何在tableview单元格中第一次点击时更新secondVC中显示的数据?

enter code here

类ViewController:UIViewController,UITableViewDataSource,UITableViewDelegate {

@IBOutlet weak var tableView: UITableView!

var titles = [String]()
var subjects = [String]()
var previews = [String]()
var textFieldsText = [UITextField!]()

var selectedTitle: String!
var selectedSubject: String!
var selectedPreview: String!


override func viewDidLoad() {
    super.viewDidLoad()


    navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "addTitle")


}

override func viewDidAppear(animated: Bool) {
    tableView.reloadData()
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
    cell.textLabel?.text = titles[indexPath.row]
    cell.detailTextLabel?.text = subjects[indexPath.row]

    return cell
}

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

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    selectedTitle = self.titles[indexPath.row]
    selectedSubject = self.subjects[indexPath.row]
    selectedPreview = self.previews[indexPath.row]
    tableView.reloadData()

}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showPreview" {
        let dataToPass = segue.destinationViewController as! previewViewController
        dataToPass.titlesString = selectedTitle
        dataToPass.subjectsString = selectedSubject
        dataToPass.previewsString = selectedPreview

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

func addTitle()  {
    let addAlert = UIAlertController(title: "New Title", message: "Add new title, subject and a short preview", preferredStyle: .Alert)

        addAlert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))

        addAlert.addTextFieldWithConfigurationHandler {[unowned self] textField in
            textField.placeholder = "Add Title"
            textField.textAlignment = .Center
            self.textFieldsText.append(textField)
            }
        addAlert.addTextFieldWithConfigurationHandler { textField in
            textField.placeholder = "Add Subject"
            textField.textAlignment = .Center
            self.textFieldsText.append(textField)
            }
        addAlert.addTextFieldWithConfigurationHandler { textField in
            textField.placeholder = "Add Short Preview"
            textField.textAlignment = .Center
            self.textFieldsText.append(textField)
            }
        addAlert.addAction(UIAlertAction(title: "Done", style: .Default){ _ in
            self.titles.append(self.textFieldsText[0].text!)
            self.subjects.append(self.textFieldsText[1].text!)
            self.previews.append(self.textFieldsText[2].text!)
            self.tableView.reloadData()
            self.textFieldsText.removeAll()
            })



    presentViewController(addAlert, animated: true, completion: nil)

}

}

class previewViewController: UIViewController {
@IBAction func readButton(sender: UIButton) {
}
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var subjectLabel: UILabel!
@IBOutlet weak var shortPreviewLabel: UITextView!

var titlesString: String!
var subjectsString: String!
var previewsString: String!



override func viewDidLoad() {
    super.viewDidLoad()
    navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Save, target: self, action: "saveChanges")

    titleLabel.text = titlesString
    subjectLabel.text = subjectsString
    shortPreviewLabel.text = previewsString

}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showDetail" {

        let dataToDetail = segue.destinationViewController as! detailViewController
        dataToDetail.textViewString = self.previewsString

    }
}

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

1 个答案:

答案 0 :(得分:0)

如果我理解你的问题,这可能有所帮助:

使用此didSelectRowAtIndexPath

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    performSegueWithIdentifier("showPreview", sender: indexPath.row)
}

使用此prepareForSegue

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showPreview" {
        let destinationViewController = segue.destinationViewController as! previewViewController
        if let index = sender as? Int {
            destinationViewController.titlesString = self.titles[index]
            destinationViewController.subjectsString = self.subjects[index]
            destinationViewController.previewsString = self.previews[index]
        }
    }
}

你需要有ViewControllerpreviewViewController的segue,名为" showPreview"。像这样:

segue instruction