gpt4 book ai didi

ios - 如何从 TableView 单元格内的文本字段获取文本并将其存储

转载 作者:行者123 更新时间:2023-11-30 12:25:40 24 4
gpt4 key购买 nike

我目前正在开发一个应用程序,但不使用 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
}
}

最佳答案

在 func save() 中

让 myCell = tableView.dequeueReusableCell(withIdentifier: "cellId") as!添加联系人单元格
错了...

使用下面的代码

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

关于ios - 如何从 TableView 单元格内的文本字段获取文本并将其存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44255454/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com