我有一个用户列表的tableview。通过选择其中一个用户,我可以在不同的控制器中加载他们的用户配置文件,包括他们的个人信息。按计划工作。
我还有一个滑动手势,让管理员编辑或删除这些用户。我正在尝试通过选择编辑按钮将用户数据发送到我的“编辑用户”控制器。Swift给出了以下错误:常量'student'在初始化之前被闭包捕获
转到我的用户配置文件控制器

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

在swipe函数下面编辑用户。
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
}
}

最佳答案

在调用之前,需要将选定的学生分配给student对象

showUserprofileDetail(student: student)

                  or

self.editUserProfileDetail(student: student).

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

具有
let student = arrayStudents[indexPath.row]

关于ios - Swift:初始化数据并将其发送到其他 Controller ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42387933/

10-09 16:33
查看更多