2015-07-31 14 views
10

मैं प्रत्येक कोशिका में textfields के साथ एक मेज दृश्य बनाना चाहते हैं,स्विफ्ट में टेक्स्टफील्ड के साथ UITableview कैसे बनाएं?

मैं एक तेज फ़ाइल में एक कस्टम वर्ग है:

import UIKit 

public class TextInputTableViewCell: UITableViewCell{ 

    @IBOutlet weak var textField: UITextField! 
    public func configure(#text: String?, placeholder: String) { 
     textField.text = text 
     textField.placeholder = placeholder 

     textField.accessibilityValue = text 
     textField.accessibilityLabel = placeholder 
    } 
} 

तब मेरे ViewController में मैं

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ 

    let cell = tableView.dequeueReusableCellWithIdentifier("TextInputCell") as! TextInputTableViewCell 

    cell.configure(text: "", placeholder: "Enter some text!") 

    text = cell.textField.text 

    return cell 

} 

है कि अच्छी तरह से काम करता है:

enter image description here

लेकिन जब उपयोगकर्ता टेक्स्टफील्ड में टेक्स्ट दर्ज करता है और बटन दबाता है तो मैं प्रत्येक टेक्स्टफील्ड के तारों को सरणी में संग्रहीत करना चाहता हूं। मैं

text = cell.textField.text 
println(text) 

साथ की कोशिश की है लेकिन यह अगर यह खाली था

मैं कैसे यह काम कर सकते हैं की तरह कुछ भी प्रिंट?

+0

का उपयोग कर पहले से ही http में जवाब मूल्यों को प्रदर्शित करने://stackoverflow.com/questions/7344247/fetching-values-from-textfield-in-a-custom-cell-iphone –

उत्तर

9

आपके विचार एक UITextFieldDelegate

बन नियंत्रक में

नियंत्रक देखें

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate { 

var allCellsText = [String]() 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomTableViewCell 

    cell.theField.delegate = self // theField is your IBOutlet UITextfield in your custom cell 

    cell.theField.text = "Test" 

    return cell 
} 

func textFieldDidEndEditing(textField: UITextField) { 
    allCellsText.append(textField.text) 
    println(allCellsText) 
} 
} 

यह हमेशा जोड़ देंगे, टेक्सास से डेटा allCellsText सरणी के लिए tField।

+0

यह काम करता है! लेकिन जब बटन दबाया जाता है तो मैं सरणी में textfield.text कैसे जोड़ सकता हूं? मैंने डालने की कोशिश की: 'allCellsText.append (textField.text) println (allCellsText)' बटन दबाए गए फ़ंक्शन के अंदर लेकिन यह टेक्स्टफील्ड – Zablah

+0

की पहचान नहीं करता है जिसे टेक्स्टफ़ील्ड प्रतिनिधि के साथ इसका उपयोग नहीं किया जा सकता है, हालांकि आप कुछ प्रकार सेट कर सकते हैं बटन के साथ अपने डेटा पर शर्त, या डेटा को एक अलग सरणी में कॉपी करें। –

+0

@thefredelement 'क्लास व्यू कंट्रोलर' में मैंने TextInputTableViewCell और ViewDidAppear में एक उदाहरण बनाया है, मैं UserDefaults से डेटा को TextInputTableViewCell से 'var textField' पर असाइन करने का प्रयास कर रहा हूं, लेकिन असाइनमेंट विफल रहता है, वैकल्पिक विकल्प को अनचाहे करते समय अनपेक्षित रूप से पाया जाता है – bibscy

0

इस विधि

text = cell.textfield.text 

सेल init, यू इस datas को बचाने के लिए मॉडल नहीं किया है, इसलिए कुछ भी नहीं है! आप ViewController में var textString init कर सकते हैं, एक इनहेरिट UITextFieldDelegate

optional func textFieldDidEndEditing(_ textField: UITextField) 
{ 
    textString = _textField.text 
} 
optional func textFieldShouldReturn(_ textField: UITextField) -> Bool{ 
    return true 
} 

और cell.textField.text = textString

1

अपने func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{} में एक चर

var arrayTextField = [UITextField]() 
इस के बाद

बनाने सेल लौटने से पहले जोड़ने

self.arrayTextField.append(textfield) 

, के बाद इस

संबंधित मुद्दे