Reputation:
My code below is just basic tableview with nothing in it. Using a textfield and button. Textfield to place text and button to submit the text. I would like the user to only be able to enter int into the array. How can I use the textfield and button to add multiple entries to this blank tableview?
import UIKit
var list = [""]
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
@IBOutlet var placeText: UITextField!
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return(list.count)
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.text = list[indexPath.row]
return(cell)
}
@IBAction func enterText(_ sender: Any) {
}}
Upvotes: 0
Views: 38
Reputation: 896
Swift 3.0
@IBAction func enterText(_ sender: Any) {
if !placeText.text!.isEmpty {
list.append(placeText.text)
tableview.reloadData()
}
}
Upvotes: 1
Reputation: 19602
Add the text from the UITextfield
to your list and reload the tableview afterwards:
@IBAction func enterText(_ sender: Any) {
let newText = textfield.text
list.append(newText)
tableView.reloadData()
}
Upvotes: 1