2015-07-31 43 views
10

Voglio fare una vista tabella con campi di testo in ogni cella,Come rendere UITableview con Textfield in swift?

Ho una classe personalizzata in un file veloce:

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 
    } 
} 

Poi nel mio ViewController devo

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 

} 

Quello funziona bene:

enter image description here

Ma quando l'utente inserisce il testo nel campo di testo e preme il pulsante Voglio memorizzare le stringhe di ogni campo di testo in una matrice. Ho provato con

text = cell.textField.text 
println(text) 

ma stampa nulla come se fosse vuota

Come posso farlo funzionare?

+0

già risposto in http://stackoverflow.com/questions/7344247/fetching-values-from-textfield-in-a-custom-cell-iphone –

risposta

9

Nel vostro controller della vista diventare un UITextFieldDelegate

View Controller

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) 
} 
} 

Questo sarà sempre aggiungerà i dati dal tex tCampo sull'array allCellsText.

+0

Funziona! Ma come posso aggiungere textfield.text alla matrice quando viene premuto il pulsante? Ho provato a mettere: 'allCellsText.append (textField.text) println (allCellsText)' all'interno del pulsantePressione funzione ma non identifica il campo di testo – Zablah

+0

Non è possibile usarlo con il delegato textField, anche se è possibile impostare un tipo di condizione sui dati con il pulsante o copiare i dati su un altro array. –

+0

@thefredelement In 'classe ViewController' ho creato un'istanza di TextInputTableViewCell e in ViewDidAppear, sto cercando di assegnare i dati da UserDefaults a' var textField' da TextInputTableViewCell, ma l'assegnazione fallisce, inaspettatamente trovato nil mentre scartando un valore facoltativo – bibscy

0

questo metodo è init della cellula, u non hanno modellare per salvare questi dati, in modo da

text = cell.textfield.text 

è niente! si può init var textString in viewcontroller, un UITextFieldDelegate eredita

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

e cell.textField.text = textString

1

creare una variabile

var arrayTextField = [UITextField]() 

dopo questo nella vostra func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{} aggiungere

self.arrayTextField.append(textfield) 

prima di tornare cella, dopo questo per visualizzare i valori utilizzando

Problemi correlati