将信息从字典推送到UITextField(Swift)

时间:2016-06-23 04:13:28

标签: swift uitableview dictionary uitextfield

我正在尝试将信息从选定的UITableviewCell学生单元格推送到视图控制器,每个信息部分(名字,姓氏等)都带有UITextField,因此可以更改但我似乎无法找到如何将字典中的信息推送到每个文本字段。我已经尝试重新分配它没有运气。

这是我的字典swift文件样本

class ClassRosterModel {

//Student Dictionary for Student infomation
var studentsRoster = [Dictionary<String, String>]()

init () {

    studentsRoster.append(["firstName": "Alex" , "lastName" : "Kaz", "major" : "SE", "email" : "s123456@school.edu", "currentTerm" : "Spring", "numberOfCredits" : "\(randomCredits[0])", "password" : "000000"])

}

这是我班级的一部分,在选择之前拥有所有细胞 与segue

class ClassRosterTableViewController: UITableViewController {

//declaring studentsList dictionary
var studentsList = [Dictionary<String, String>]()

var selectedRowIndex = 0



    //Assignment of studentsList and myStudentRoster
    let myStudentRoster = ClassRosterModel()
    studentsList = myStudentRoster.studentsRoster



//declaring cell with identifier from studentCell.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("studentCell", forIndexPath: indexPath)

    // Configuring the cell with student information
    cell.textLabel?.text = "\(studentsList[indexPath.row]["lastName"]!), \(studentsList[indexPath.row]["firstName"]!) - \(studentsList[indexPath.row]["email"]!)"
    cell.detailTextLabel?.text = "\(studentsList[indexPath.row]["currentTerm"]!) - \(studentsList[indexPath.row]["numberOfCredits"]!) credits"


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    let dvc = segue.destinationViewController as! StudentInfoViewController

    let selectedStudent = studentsList[selectedRowIndex]

    dvc.studentRecord = selectedStudent

}

我刚刚在文本字段中输入了第一个名字 学生信息页面

class StudentInfoViewController: UIViewController {

@IBOutlet weak var FirstNameTextField: UITextField!

var studentRecord = Dictionary<String, String>()


override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

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

1 个答案:

答案 0 :(得分:0)

这都是因为你的财产的错误类型studentsList。 您应该将studentsList更改为存储字典列表的类型数组。

此外,以下代码完全错误

//Assignment of studentsList and myStudentRoster
let myStudentRoster = ClassRosterModel()
studentsList = myStudentRoster.studentsRoster

我不知道你在这做什么。但是你不能将字典分配给数组。