2017-05-30 9 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!) 
} 
} 

そして、ここでは、Imは

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) 

} 

EDITEDやろうとするものである:(セーブ・FUNCで 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私はちょうどコードを編集することができますか? thanksss – Van

+0

多分、私はコード全体を理解できませんでしたが、save-MethodでUI /コントローラを生成しているようです。それは奇妙に見えます。 – Gerriet

答えて

0

)を

let myCell = tableView.dequeueReusableCell(withIdentifier: "cellId") as! AddContactCell
は間違っている...コード

let myCell = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) as! AddContactCell 
関連する問題