2017-05-30 49 views
0

我目前正在開發應用程序而不使用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 
     } 
    } 
+0

可以請你分享 「CellforRow」 的方法? –

+0

@DimpleShah我只是編輯代碼,你可以看看嗎?謝謝 – Van

+0

也許我不明白整個代碼,但似乎你在save-Method中生成你的UI /控制器。這看起來很奇怪。 – Gerriet

回答

0

在FUNC保存()

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

使用下面的代碼

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