将整个文本字段添加到UITableView中以自动填充 [英] add entires from textfield to UITableView to automatically populate

查看:81
本文介绍了将整个文本字段添加到UITableView中以自动填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我到目前为止的代码.我能够输入文本字段并将其显示在UITableView中,但是只有在我重新加载页面并返回到它之后才可以.我要输入文本字段,然后单击添加"以使其自动出现在UITableView中.

This is the code I have so far. I am able to enter into the textfield and have it appear in the UITableView, but only after I reload the page and come back to it. I want for what I enter into the textfield and when I click 'add' for it to automatically appear in the UITableView.

@IBOutlet var itemTextField: UITextField!
@IBOutlet var table: UITableView!

var items: [String] = []

@IBAction func add(_ sender: Any) {
    let itemsObject = UserDefaults.standard.object(forKey: "items")

    var items:[String]

    if let tempItems = itemsObject as? [String] {
        items = tempItems
        items.append(itemTextField.text!)

        print(items)
    } else {
        items = [itemTextField.text!]
    }

    UserDefaults.standard.set(items, forKey: "items" )
    itemTextField.text = ""
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.view.endEditing(true)
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    textField.resignFirstResponder()

    return true
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return items.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")

    cell.textLabel?.text = items[indexPath.row]
    cell.textLabel?.font = UIFont(name: "Type02", size: 20)

    return cell
}

override func viewDidAppear(_ animated: Bool) {
    let itemsOBject = UserDefaults.standard.object(forKey: "items")

    if let tempItems = itemsOBject as? [String]{
        items = tempItems
    }

    table.reloadData()
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == UITableViewCellEditingStyle.delete {
        items.remove(at: indexPath.row)

        table.reloadData()

        UserDefaults.standard.set(items, forKey: "items")
    }
}

推荐答案

它对我有用.

textfieldtableview添加插座.

 @IBOutlet weak var nametextField: UITextField!
 @IBOutlet weak var dataTableView: UITableView!

添加操作button添加IBAction.

@IBAction func addNameToTable(_ sender: Any) {

        // Check for value in nameTextField
        guard let profilename = nametextField.text else{
          return
        }
        // Append text field value in data array
        data.append(profilename)
       // Reload table to show text field name in table
        dataTableView.reloadData()
    }

创建array来保存字符串值-:

Create array to hold string values-:

var data = [String]()

添加table方法以在单击添加"按钮时填充表中的数据.

Add table methods to populate data on table on clicking Add button.

func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }// Default is 1 if not implemented

    // return array count
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return data.count
    }

    // dequeue cell
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        cell.textLabel?.text = data[indexPath.row]
        return cell
    }

    // Row height
    public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
        return 50
    }

完整代码-:

import UIKit
import AVKit
class ViewController: UIViewController {

    @IBOutlet weak var nametextField: UITextField!
    @IBOutlet weak var dataTableView: UITableView!
    var data = [String]()

    //MArk-: ViewDidLoad
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func addNameToTable(_ sender: Any) {

        guard let profilename = nametextField.text else{
          return
        }
        data.append(profilename)
        dataTableView.reloadData()
    }
}

extension ViewController : UITableViewDelegate,UITableViewDataSource{

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }// Default is 1 if not implemented

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return data.count
    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") 
        cell.textLabel?.text = data[indexPath.row]
        return cell
    }

    public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
        return 50
    }
}

这篇关于将整个文本字段添加到UITableView中以自动填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆