How to make UITableview with Textfield in swift?

前端 未结 4 542
广开言路
广开言路 2020-12-15 21:30

I want to make a table view with textfields in each cell,

I have a custom class in a swift file:

import UIKit

public class TextInputTableViewCell: U         


        
4条回答
  •  甜味超标
    2020-12-15 22:14

    In your view controller become a 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)
        }
    }
    
    

    This will always append the data from the textField to the allCellsText array.

提交回复
热议问题