已经在这个待办事项应用程序上滞留了几天。所以我试图输入一个";做";在文本字段中。然后单击保存按钮将其发送到表视图。但不幸的是,我们只能通过保存按钮获得文本字段来打印输入文本。但我们非常感谢任何帮助。前两个";添加";字符串已添加到tableView中,只是为了寻求帮助。
import UIKit
class ViewController: UIViewController, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource {
//UIViewController, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource
var added = [String]()
var added2: String = "";
var added3: String = "";
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
added = ["dog", "dog2"];
let savedToDo = UserDefaults.standard.object(forKey: "ThingAdded")
if let savedToList = savedToDo as? UITableView {
tableView? = savedToList
// cells.text = savedToList
}
}
// var added: String = "";
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return added.count
}
//savedToList
//var transfer = textField.text
func tableView(_ tableView: UITableView, commit deleteSlider: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if deleteSlider == .delete {
added.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
} else if deleteSlider == .insert {
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cells = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "TDList")
cells.textLabel?.text = added[indexPath.row]
return cells
}
/* override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "doneSegue" {
added2 = textField.text!
}
}
*/
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var textField: UITextField!
@IBAction func saveButton(segue: UIStoryboardSegue) {
//_ sender: Any
added3 = textField.text!
//textField.text! == tableView?
// var returns = textField.text;
// textField.text! == tableView
//let additions = segue.source as! tableViewAddition
/* if let table = tableView {
print(table);
}
*/
// performSegue(withIdentifier: "endSegue", sender: nil)
// let theView = segue.source as! tableViewAddition
// added2 = theView.name
// if segue.identfier == "endSegue" {
//added3 = added2.text!
// let controlled = segue.destinationViewController as! tabkeViewAddition
// returns = transfer
UserDefaults.standard.set(textField.text!, forKey: "ThingAdded")
/* if textField >= 1 {
saveOutputButton.isHidden = false;
} else {
saveOutputButton.isHidden = true;
}
*/
/* if textField.text isEqualToString: @"" == TRUE {
saveOutputButton setEnabled: NO
}
*/
/* if textField.text!.isEmpty {
saveOutputButton.isHidden = true;
} else {
saveOutputButton.isHidden = false;
}
*/
added.append(added2)
if let field = textField.text {
print(field);
tableView?.reloadData()
}
}
@IBOutlet weak var saveOutputButton: UIButton!
// right handed and left handed mode
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true);
}
public func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true;
}
}
您需要设置
tableView.delegate = self
tableView.dataSource = self
我还注意到,在saveButton
函数中,您将文本保存到added3,但将added2添加到数组中。如果将文本保存到added3,则必须将added3添加到数组中。
制作待办事项列表应用程序是学习tableViews和持久数据基础知识的好方法。我强烈推荐这个教程视频。该频道还有很多其他视频,是iOS和Swift初学者的绝佳资源。