我正在尝试在 Xcode 上创建一个程序,该程序允许用户通过文本字段(单击按钮时)将多个数据输入到表格 View 中。添加数据时,我希望在应用程序关闭后将其存储而不是删除 - 对于这一部分,我相信我必须使用 NSUserDefaults,但是,我不确定如何保存字符串数组? (我只熟悉存储单个字符串)。
This is what my view controller currently looks like.
我根本没有在我的 View Controller 上做太多事情,但这就是它目前所拥有的。
import UIKit
class NewViewController: UIViewController {
@IBOutlet weak var text: UITextField!
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
最佳答案
尝试以下操作:
UITextField
输入的所有文本(即 var array = [String]()
add
的行动中按钮,将用户在文本字段中输入的文本附加到数组中。if text.text != "" && !text.text.isEmpty {
// append the text to your array
array.append(text.text!)
text.text = "" // empty the `UITextField`
}
tableView
方法,使 numberOfRows
return array.count
只需添加 UILabel
为您定制UITableViewCell
这将在单独的单元格中显示数组中每个输入的项目。 关于ios - 如何从 UITextField 向 UITableView 添加多个数据? ( swift 4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51916034/