Ios 将entires从textfield添加到UITableView以自动填充
这是我目前掌握的代码。我可以进入文本字段并将其显示在UITableView中,但只有在我重新加载页面并返回到它之后。我想知道我在文本字段中输入的内容,当我单击“添加”时,它会自动显示在UITableView中Ios 将entires从textfield添加到UITableView以自动填充,ios,swift,uitableview,uitextfield,Ios,Swift,Uitableview,Uitextfield,这是我目前掌握的代码。我可以进入文本字段并将其显示在UITableView中,但只有在我重新加载页面并返回到它之后。我想知道我在文本字段中输入的内容,当我单击“添加”时,它会自动显示在UITableView中 @IBOutlet var itemTextField: UITextField! @IBOutlet var table: UITableView! var items: [String] = [] @IBAction func add(_ sender: Any) { le
@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")
}
}
@IBOutlet var itemTextField:UITextField!
@IBOUTLE var表:UITableView!
变量项:[字符串]=[]
@iAction func add(\发送方:任意){
让itemsObject=UserDefaults.standard.object(forKey:“items”)
变量项:[字符串]
如果让tempItems=itemsObject为?[字符串]{
项目=临时项目
items.append(itemTextField.text!)
打印(项目)
}否则{
items=[itemTextField.text!]
}
UserDefaults.standard.set(items,forKey:“items”)
itemTextField.text=“”
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
self.view.endEditing(true)
}
func textField应该返回(textField:UITextField)->Bool{
textField.resignFirstResponder()辞职
返回真值
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回项目。计数
}
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
let cell=UITableViewCell(样式:UITableViewCellStyle.default,reuseIdentifier:“单元格”)
cell.textlab?.text=项[indexPath.row]
cell.textLabel?.font=UIFont(名称:“Type02”,大小:20)
返回单元
}
覆盖函数视图显示(u动画:Bool){
让itemsOBject=UserDefaults.standard.object(forKey:“items”)
如果让tempItems=itemsOBject为?[字符串]{
项目=临时项目
}
表.重新加载数据()
}
func tableView(tableView:UITableView,commit editingStyle:UITableViewCellEditingStyle,forRowAt indexPath:indexPath){
如果editingStyle==UITableViewCellEditingStyle.delete{
items.remove(位于:indexPath.row)
表.重新加载数据()
UserDefaults.standard.set(items,forKey:“items”)
}
}
每当项目数组更改时,您需要告诉表视图重新加载数据()。因此,请尝试将table.reloadData()
添加到add
函数的末尾。这对我很有用
为textfield
和tableview
添加插座
@IBOutlet weak var nametextField: UITextField!
@IBOutlet weak var dataTableView: UITableView!
添加iAction
用于添加操作按钮
@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()
}
创建数组
以保存字符串值-:
var data = [String]()
单击“添加”按钮,添加table
方法以填充表中的数据
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
}
}
在添加函数上重新加载表,然后在数组中追加数据之后,改为在ViewDidDisplay中执行。我尝试了此解决方案,但没有成功。。。我必须返回一个ViewController,然后返回带有textfield和TableView的ViewController,然后它才会出现。。。任何其他想法…我尝试了这个解决方案,但没有运气。。。我必须返回一个ViewController,然后返回带有textfield和TableView的ViewController,然后它才会出现。。。还有其他想法吗。。。