找到你要的答案

Q:didSelectRowAtIndexPath is not changing the property I am passing to a previous View

Q:didselectrowatindexpath不会改变我通过以前查看属性

I am trying to pass data from a tableview to a previous screen, which is my Sign Up screen. Specifically what I am sending is the name of a school.

What is happening is that when I return to the previous screen through the segue, the label I am changing (schoolTextLabel) is showing "None Selected", when I want it to display the school name. Through some error checking, I have found that when I have it println(theSchoolName), it is correctly choosing the name I want displayed, It is just not passing the value to the other screen.

Please help!
I have searched endlessly on the internet for the answer and cannot for the life of me find the answer.
Thank you!

TableViewController

var namesArray = [String]()
var locationsArray = [String]()

var theSchoolName = "None Selected"


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "selectedSchool" {
        var completeSignUpVC = segue.destinationViewController as! SignUpViewController
        completeSignUpVC.school = self.theSchoolName
    }
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.theSchoolName = self.namesArray[indexPath.row]
    self.performSegueWithIdentifier("selectedSchool", sender: self.namesArray[indexPath.row])

    println(theSchoolName)
}

SignUpViewController

class SignUpViewController: UIViewController, UITextFieldDelegate {

@IBOutlet var schoolTextLabel: UILabel!
var school = "No School Selected"

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib. 
    self.schoolTextLabel.text = self.school        
    usernameSignupText.delegate = self
    passwordSignupText.delegate = self   
}
}

我试图把数据从一个TableView前一个屏幕,这是我的登录屏幕。具体地说,我发送的是一所学校的名字。

正在发生的事情是,当我返回前一个屏幕通过跳转,标签我改变(schooltextlabel)呈现的是“没有选择”,我想用它来显示学校的名字。通过一些错误检查,我发现,当我有了println(theschoolname),它是正确的选择我想要显示的名称,它不是通过价值其他屏幕。

Please help!
I have searched endlessly on the internet for the answer and cannot for the life of me find the answer.
Thank you!

了TableViewController

var namesArray = [String]()
var locationsArray = [String]()

var theSchoolName = "None Selected"


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "selectedSchool" {
        var completeSignUpVC = segue.destinationViewController as! signupviewcontroller
        completeSignUpVC.school = self.theSchoolName
    }
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.theSchoolName = self.namesArray[indexPath.row]
    self.performSegueWithIdentifier("selectedSchool", sender: self.namesArray[indexPath.row])

    println(theSchoolName)
}

signupviewcontroller

class signupviewcontroller: UIViewController, UITextFieldDelegate {

@IBOutlet var schoolTextLabel: UILabel!
var school = "No School Selected"

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib. 
    self.schoolTextLabel.text = self.school        
    usernameSignupText.delegate = self
    passwordSignupText.delegate = self   
}
}
answer1: 回答1:

I found the answer. I needed to find the selected row index, and then call that number in the namesArray. I'm not exactly sure why my previous code didn't work, but this now works.

var selectedRowIndex = self.schoolTable.indexPathForSelectedRow()
var completeSignUpVC = segue.destinationViewController as! SignUpViewController
completeSignUpVC.school = namesArray[selectedRowIndex!.row]

我找到了答案。我需要找到选定的行的索引,然后调用在namesarray数量。我不知道为什么我以前的代码没有工作,但现在工作。

var selectedRowIndex = self.schoolTable.indexPathForSelectedRow()
var completeSignUpVC = segue.destinationViewController as! signupviewcontroller
completeSignUpVC.school = namesArray[selectedRowIndex!.row]
ios  uitableview  didselectrowatindexpath