如何从表视图单元格中的文本字段获取文本并将其存储

时间:2017-05-30 06:53:28

标签: ios swift uitableview

我目前正在开发一款不使用UIStoryboard的应用。我有一个带有4个UITextField对象的简单表单,可以从用户那里获得输入(名字,姓氏等)。

我想获取值并将它们存储在我的模型中。但是,它似乎总是空的。如何在不使用UIStoryBoard的情况下获取值?这是我的模特:

class Records: NSObject, NSCoding {

//MARK: Properties

var firstName: String
var lastName:String
var occupation:String
var placeMeet: String
var notes:String

//MARK: Archiving Paths
static let DocumentsDirectory = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first!
static let ArchiveURL = DocumentsDirectory.appendingPathComponent("records")

//MARK: Types

struct PropertyKey {
    static let firstName = "firstName"
    static let lastName = "lastName"
    static let occupation = "occupation"
    static let placeMeet = "placeMeet"
    static let notes = "notes"
}

//MARK: Initialization

init?(firstName: String, lastName:String, occupation: String, placeMeet: String, notes: String) {
    // The name must not be empty
    guard !firstName.isEmpty else {
        return nil
    }
    guard !lastName.isEmpty else {
        return nil
    }
    guard !occupation.isEmpty else {
        return nil
    }
    guard !placeMeet.isEmpty else {
        return nil
    }
    guard !notes.isEmpty else {
        return nil
    }

    // Initialization should fail if there is no name or if the rating is negative.
    if firstName.isEmpty || lastName.isEmpty  {
        return nil
    }

    // Initialize stored properties.
    self.firstName = firstName
    self.lastName = lastName
    self.occupation = occupation
    self.placeMeet = placeMeet
    self.notes = notes
}

//MARK: NSCoding

func encode(with aCoder: NSCoder) {
    aCoder.encode(firstName, forKey: PropertyKey.firstName)
    aCoder.encode(lastName, forKey: PropertyKey.lastName)
    aCoder.encode(occupation, forKey: PropertyKey.occupation)
    aCoder.encode(placeMeet, forKey: PropertyKey.placeMeet)
    aCoder.encode(notes, forKey: PropertyKey.notes)
}

required convenience init?(coder aDecoder: NSCoder) {
    // The name is required. If we cannot decode a name string, the initializer should fail.
    guard let firstName = aDecoder.decodeObject(forKey: PropertyKey.firstName) as? String else {
        os_log("Unable to decode the name for a Record object.", log: OSLog.default, type: .debug)
        return nil
    }
    guard let lastName = aDecoder.decodeObject(forKey: PropertyKey.lastName) as? String else {
        os_log("Unable to decode the name for a Record object.", log: OSLog.default, type: .debug)
        return nil
    }

    // Because photo is an optional property of Meal, just use conditional cast.
    let occupation = aDecoder.decodeObject(forKey: PropertyKey.occupation) as? String
    let placeMeet = aDecoder.decodeObject(forKey: PropertyKey.placeMeet) as? String
    let notes = aDecoder.decodeObject(forKey: PropertyKey.notes) as? String

    // Must call designated initializer.
    self.init(firstName:firstName , lastName: lastName, occupation: occupation!, placeMeet: placeMeet! , notes:notes!)
}
}

这就是我想要做的事情

func save() {
    let homepage = HomepageController()
    let navController = UINavigationController(rootViewController: homepage) // Creating a navigation controller with VC1 at the root of the navigation stack
    let myCell = tableView.dequeueReusableCell(withIdentifier: "cellId") as! AddContactCell
    let note = tableView.dequeueReusableCell(withIdentifier: "note") as! Note
    var occu = ""
    var fname = ""
    var lname = ""
    var place = ""
    var noteString = ""

    self.navigationController?.present(navController, animated: true)
    if myCell.nameLabel.text == items[0] {
        occu = myCell.input_field.text!
    }
    else if myCell.nameLabel.text == items[1] {
        fname = myCell.input_field.text!
    }
    else if myCell.nameLabel.text == items[2] {
        lname = myCell.input_field.text!
    }
    else if myCell.nameLabel.text == "Place" {
        place = myCell.input_field.text!
    }
    else {
        noteString = note.input_field.text!
    }

    record = Records(firstName:fname , lastName: lname, occupation: occu, placeMeet:place, notes: noteString)

}

编辑: cellForRow覆盖功能

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let myCell = tableView.dequeueReusableCell(withIdentifier: "cellId") as! AddContactCell
    let note = tableView.dequeueReusableCell(withIdentifier: "note") as! Note
    myCell.input_field.delegate = self
    note.input_field.delegate = self as? UITextViewDelegate
    if indexPath.section == 0 {
        myCell.nameLabel.text = items[indexPath.row]
        if (myCell.nameLabel.text == "Occupation")
        {
            myCell.input_field.attributedPlaceholder = NSAttributedString(string: "Occupation",


    attributes: [NSForegroundColorAttributeName:UIColor(red:0.81, green:0.81, blue:0.83, alpha:1.0)])
            }
            else if (myCell.nameLabel.text == "First Name"){
                myCell.input_field.attributedPlaceholder = NSAttributedString(string: "First Name",
attributes: [NSForegroundColorAttributeName:UIColor(red:0.81, green:0.81, blue:0.83, alpha:1.0)])
            }
            else {
                myCell.input_field.attributedPlaceholder = NSAttributedString(string: "Last Name",
attributes: [NSForegroundColorAttributeName:UIColor(red:0.81, green:0.81, blue:0.83, alpha:1.0)])
            }
            myCell.myContactController = self
            return myCell
        }
        else if indexPath.section == 1 {
            myCell.nameLabel.text = "Place"
            myCell.input_field.attributedPlaceholder = NSAttributedString(string: "Place",
attributes: [NSForegroundColorAttributeName:UIColor(red:0.81, green:0.81, blue:0.83, alpha:1.0)])
            return myCell
        }
        else {
            return note
        }
    }

1 个答案:

答案 0 :(得分:0)

在func save()

let myCell = tableView.dequeueReusableCell(withIdentifier: "cellId") as! AddContactCell
 是错的......

使用以下代码

let myCell = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) as! AddContactCell