Swift:初始化数据并将其发送到不同的控制器

时间:2017-02-22 09:44:37

标签: ios swift initialization

我有一个包含用户列表的tableview。通过选择其中一个用户,我可以在不同的控制器中加载他们的用户配置文件,包括他们的个人信息。按预期工作。

我还有一个轻扫手势,让管理员编辑或删除这些用户。我目前正在尝试通过选择编辑按钮将用户数据发送到我的“编辑用户”控制器。 Swift给了我以下错误: 在初始化之前由闭包捕获的常量'学生'

选择我的用户个人资料控制器

class Student: NSObject {
    var profileImage: String
    var firstName: String

init(profileImage: String, firstName: String) {
    self.profileImage = profileImage
    self.firstName = firstName
  }
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let student: Student
    showUserprofileDetail(student: student)
}

func showUserprofileDetail(student: Student) {

    let detailViewController = UserProfileController()
    detailViewController.student = student

    present(detailViewController, animated: true, completion: nil)
}

在我的用户配置文件控制器中,我使用以下方法加载数据:

var student: Student? {
        didSet {
             if let profileImage = student?.profileImage {
                 profileImageView.image = UIImage(named: profileImage)
             } 

            if let firstName = student?.firstName {
                 nameLabelView.text = firstName
             }
    }

在滑动功能下方编辑用户。

override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

    // Edit student
    let student: Student

    let edit = UITableViewRowAction(style: .normal, title: "Edit") { action, index in

        tableView.isEditing = true

        self.editUserProfileDetail(student: student)
    }

    edit.backgroundColor = UIColor.lightGray
    return [edit]
}

func editUserProfileDetail(student: Student) {

    let detailViewController = EditStudentController()
    detailViewController.student = student

    self.present(detailViewController, animated: true, completion: nil)
}

新问题: 所有数据都被正确地推送到新控制器,但只有配置文件图像可见。我还希望将数据推送到我的所有文本字段。我的文本字段是tableview中的自定义类。如何访问这些文本字段?

let editStudentTableViewController = EditStudentInfoController()

    var student: Student? {
        didSet {
            if let profileImage = student?.profileImage {
                profileImageView.image = UIImage(named: profileImage)
            }

            if let firstName = student?.firstName {
                editStudentTableViewController.editStudentFirstNameTextField?.text? = firstName
            }
        }
    }

 lazy var profileImageView: UIImageView = {
    let imageView = UIImageView()
    imageView.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
    imageView.image = UIImage(named: "profile_default")
    imageView.isUserInteractionEnabled = true
    imageView.layer.masksToBounds = false
    imageView.layer.cornerRadius = imageView.frame.height/2
    imageView.clipsToBounds = true
    imageView.translatesAutoresizingMaskIntoConstraints = false
    return imageView
}()

// MARK: Custom cells
class EditStudentFirstNameCell: UITableViewCell {

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
    setupViews()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

let editStudentFirstNameTextField: UITextField = {
    let editStudentFirstNameTextField = UITextField()
    editStudentFirstNameTextField.placeholder = "First Name"
    editStudentFirstNameTextField.textColor = .red
    editStudentFirstNameTextField.translatesAutoresizingMaskIntoConstraints = false
    editStudentFirstNameTextField.rightViewMode = UITextFieldViewMode.always
    editStudentFirstNameTextField.autocorrectionType = .no
    return editStudentFirstNameTextField
}()

func setupViews() {

    addSubview(editStudentFirstNameTextField)

    editStudentFirstNameTextField.rightAnchor.constraint(equalTo: self.rightAnchor, constant: 10).isActive = true
    editStudentFirstNameTextField.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
    editStudentFirstNameTextField.widthAnchor.constraint(equalToConstant: 200).isActive = true
    editStudentFirstNameTextField.heightAnchor.constraint(equalToConstant: 48).isActive = true
}
}

1 个答案:

答案 0 :(得分:2)

您需要在致电

之前将所选学生分配给您的学生对象
showUserprofileDetail(student: student)

                  or

self.editUserProfileDetail(student: student).

假设您的学生数组是 arrayStudents [Student] ,您在表数据源中使用它来显示表中学生的信息。替换您的代码行:

let student:Student

let student = arrayStudents[indexPath.row]