2016-08-03 16 views
0

私は素早く作成する必要があります2つのプロトタイプのセルを持つ1つのセルにはラベルとテキストフィールドがあり、他のセルには保存ボタンがあり、@IBOutletと@IBActionはテキストフィールドに何かを書きたい場合、保存ボタンをクリックすると、textField.textでlabel.textを変更する必要がありますが、ボタンをクリックすると問題が発生します.textFieldの値はnilです。私はすぐに迅速に、どのようにこの問題を解決することができますか? おかげ保存ボタン付きのUITableViewCell。 textFieldとラベル

+0

なぜあなたは、 2つの異なる細胞でこれを行う? 2つの細胞だけがありますか?いくつかのコードを表示する –

答えて

0

は、シナリオがこの写真 にenter image description here

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return 1 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 2 
} 


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    if indexPath.row == 0 { 
     let cell = tableView.dequeueReusableCellWithIdentifier("cella", forIndexPath: indexPath) as! TableViewCell 
     return cell 
    } 
    let cell = tableView.dequeueReusableCellWithIdentifier("saveCell", forIndexPath: indexPath) as! TableViewCell 
    return cell 
} 

とTableViewCell.swift

import UIKit 

クラスという名前の二番目のファイル内でこの2つのファイル、TableViewController.swiftを持っていますTableViewCell:UITableViewCell {

@IBOutlet weak var textField: UITextField! 
@IBOutlet weak var label: UILabel! 

override func awakeFromNib() { 
    super.awakeFromNib() 
    // Initialization code 
} 

override func setSelected(selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 

    // Configure the view for the selected state 
} 

@IBAction func saveBtn(sender: AnyObject) { 
    self.label.text = textField.text 
} 

}

0

私はそれが私の問題を解決するタグ

cell.propertyName.tag = indexPath.row 

と、このデリゲート

func textField(textField: UITextField, shouldChangeCharactersInRange range:NSRange, replacementString string: String) -> Bool { 
    if textField.tag == 0 { 
     CoreDataController.sharedIstanceCData.matsDataField = "" + textField.text!+string 
    } else { 
     CoreDataController.sharedIstanceCData.commentDataField = "" + textField.text!+string 
    } 
    return true 
} 

で問題を解決した、TNXすべて

関連する問題