How to you save a string to a table View? - ios

This is the code:
import UIKit
var reminder = [String]()
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var textField: UITextField!
#IBAction func checkMarkTapped(sender: AnyObject) {
reminder.append(textField.text!)
textField.text = ""
}
#IBAction func addReminder(sender: AnyObject) {
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return reminder.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:CoustemCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! CoustemCell
cell.tableViewLabel.text = reminder[indexPath.row]
return cell
}
}
So I when ever you press on the checkMarkTapped it would add whatever is being put in the textField to the table View. But the code that I put in the checkMarkTapped is suppose to work but keeps crashing.
So what am I doing wrong?

import UIKit
class ViewController: UIViewController
{
var reminder = [String]()
#IBOutlet weak var tbl_Update: UITableView!
override func viewDidLoad()
{
super.viewDidLoad()
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
#IBOutlet weak var textField: UITextField!
#IBAction func checkMarkTapped(sender: AnyObject)
{
reminder.append(textField.text!)
textField.text = ""
tbl_Update.reloadData()
}
#IBAction func addReminder(sender: AnyObject)
{
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return reminder.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell:CoustemCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! CoustemCell
cell.tableViewLabel.text = reminder[indexPath.row]
return cell
}
}

Related

tableview doesn't show anything?

I made sure that my cell identifier was correct I'm not too sure what the problem is. I've been rereading the code on this viewController and I'm not sure if I'm missing something or if there's a specific reason why my tableview isn't loading.
import UIKit
import JGProgressHUD
class BasketViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.tableFooterView = footerView
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//MARK: TO DO CHECK IF USER IS LOGGED IN
loadBasketFromFirestore()
}
//MARK: VARS
var basket : basket?
var allItems : [Item] = []
var purchaseItemID : [String] = [] //holds id of items you want to purchase
var hud = JGProgressHUD(style: .dark)
//MARK: IBOUTLETS
#IBOutlet weak var totalPriceLabel: UILabel!
#IBOutlet weak var totalItemsInBasket: UILabel!
#IBOutlet weak var checkOutButton: UIButton!
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var footerView: UIView!
//MARK: IBACTIONS
#IBAction func checkOutButtonTapped(_ sender: Any) {}
//something is wrong with
//MARK: DOWNLOAD BASKET
private func loadBasketFromFirestore(){
//MARK: CHANGE 1234 TO A USER ID STRING
downloadBasketFromFirestore("1234") { (basket) in
self.basket = basket
self.getBasketItems()
}
}
private func getBasketItems(){
if (basket != nil) {
print("getting items")
downloadItems(_withIDS: basket!.itemID) { (allItems) in
self.allItems = allItems
self.tableView.reloadData()
}
} else { print("basket is nil")}
}
}
extension BasketViewController : UITableViewDataSource , UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return allItems.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "basketCell", for: indexPath) as! ITEMTableViewCell
cell.generateCellForITEMS(item: allItems[indexPath.row])
return cell
}
}

How to create new cells when button on cell is pressed in tableview

*I want to insert new rows each time when add button is pressed ( condition: a valid email address is entered).
enter image description here
Here is my code:
import UIKit
var totalCells=0
class AddEmailTableCell: UITableViewCell,UITextFieldDelegate {
var obj:UIViewController=ViewController()
#IBOutlet weak var textField: UITextField!
#IBOutlet weak var addButton: UIButton!
#IBOutlet weak var addEmailLabel: UILabel!
#IBOutlet weak var addEmailImg: UIImageView!
func isValidEmail(testStr:String) -> Bool {
// print("validate calendar: \(testStr)")
let emailRegEx = "[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,}"
let emailTest = NSPredicate(format:"SELF MATCHES %#", emailRegEx)
return emailTest.evaluate(with: testStr)
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
addButton.isHidden=true
self.textField.delegate=self
}
#IBAction func addEmailButton(_ sender: Any) {
if(textField.text == nil || (textField.text?.isEmpty)! ) {
print("email not filled")
return
}
if (isValidEmail(testStr: textField.text!)==false){
print("email not valid")
return
}
totalCells=totalCells+1
let index=IndexPath(row: totalCells, section: 0)
(obj as! ViewController).tableView.insertRows(at: [index], with: UITableViewRowAnimation.automatic)
print(totalCells)
print("valid")
}
}
My viewController code is given here:
import UIKit
class ViewController:
UIViewController,UITableViewDelegate,UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
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.
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if totalCells==0{
return 1
}
return totalCells
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell=tableView.dequeueReusableCell(withIdentifier: "addEmail", for: indexPath) as! AddEmailTableCell
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 60
}
}
so what should i do here, I am calling tableview.reloadData and passing updated number of cells to my tableview, but it is not working.
You should move isValidEmail() and addEmailButton() to your ViewController and control tableview in this controller.

tableview using array not working - xcode 7.3

I'm designing an app for my school and I am trying to make a simple directory using a tableview within a view controller. I began by trying to make objects, but then switched to a more simple array, still without any luck. I do not know if it is a problem with the code or with the storyboard. Here is the code for the viewcontroller:
import UIKit
class DirectoryViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var backButton2: UIButton!
#IBOutlet weak var directoryTableView: UITableView!
// #IBOutlet weak var nameLabel: NSLayoutConstraint!
// let searchController = UISearchController(searchResultsController: nil)
// var directoryObjects: NSMutableArray! = NSMutableArray()
var names = ["Teacher 1", "Teacher 2", "Teacher 3"]
override func viewDidLoad() {
super.viewDidLoad()
/*
self.directoryObjects.addObject("Teacher 1")
self.directoryObjects.addObject("Teacher 2")
self.directoryObjects.addObject("Teacher 3")
self.directoryTableView.reloadData()
*/
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// Mark - tableview
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return 3
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let directoryCell = self.directoryTableView.dequeueReusableCellWithIdentifier("directoryCell", forIndexPath: indexPath) as! DirectoryTableViewCell
// directoryCell.directoryLabel.text = self.directoryObjects.objectAtIndex(indexPath.row) as? String
directoryCell.directoryLabel.text = names[indexPath.row]
return directoryCell
}
#IBAction func backButton2Tapped(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
}
Here is the code for the DirectoryTableViewCell:
import UIKit
class DirectoryTableViewCell: UITableViewCell {
#IBOutlet weak var directoryLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
/*
func directoryTableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
}
*/
}
You have to set the delegates. The easiest way is in the code.
In viewDidLoad add:
directoryTableView.delegate = self
directoryTableView.dataSource = self
This will allow those tableView functions to get called.
I'd also recommend changing:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return 3
}
to:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return names.count
}

show textfields text in a UITableView using swift

I'm very new in swift programming .
I'm just trying some features of textfield .
just want to show textfields value in a UItableView.
stuck in this code
please check this out ....
and give solution
import UIKit
class langViewController: UIViewController {
var txt = ""
let simpleTableIdentifier = "TableViewCell";
#IBOutlet weak var txtlang: UITextField!
#IBOutlet weak var langTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
#IBAction func addButtonTapped(sender: AnyObject)
{
txt = txtlang.text
self.langTableView.reloadData()
langData.append(txt)
print(langData)
txtlang.resignFirstResponder()
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cell = tableView.dequeueReusableCellWithIdentifier(simpleTableIdentifier) as UITableViewCell
cell.textLabel?.text = txt
return cell;
}
To update your table view cells you have to call reloadData(). So you have to do something like this:
1. Declare the txt variable right where your class definition starts:
var txt = ""
2. In your button handler call reloadData() to update your cells.
#IBAction func addButtonTapped(sender: AnyObject) {
txt = txtlang.text
self.tableView.reloadData()
}
3. Set the text to your cell's label:
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cell = tableView.dequeueReusableCellWithIdentifier(simpleTableIdentifier) as! UITableViewCell
cell.textLabel?.text = txt
return cell;
}
thanks #flashadvanced
i was using a textfield and a tableview
i trying to get text in tableView from textfield. after suggestion of #flashadvanced i did it.
my code is :
class langViewController: UIViewController,UITableViewDelegate, UITableViewDataSource{
var txt = ""
let simpleTableIdentifier = "TableViewCell";
#IBOutlet weak var txtlang: UITextField!
#IBOutlet weak var langTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.langTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
self.langTableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return langData.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = self.langTableView
.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.textLabel?.text = langData[indexPath.row]
return cell
}
#IBAction func addButtonTapped(sender: AnyObject)
{
txt = txtlang.text
langData.append(txt)
print(langData)
txtlang.resignFirstResponder()
self.langTableView.reloadData()
}

Correct way to publish model to delegated view?

I have a UITableViewController which provides an array of strings as data source:
import UIKit
class ItemsViewController: UITableViewController {
let items = [
"Bob",
"Joe",
]
#IBAction
func unwindToSegue(segue: UIStoryboardSegue) {
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Item", forIndexPath: indexPath) as UITableViewCell
cell.textLabel.text = items[indexPath.row]
return cell
}
}
I now added a second scene with a UIViewController which is activated when the user clicks on a UITableCell through a show segue.
I would now like to take the string assigned with the former UITableCell and inserted into the ItemViewController:
import UIKit
class ItemViewController: UIViewController {
#IBOutlet
weak var nameLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
//var name = segue.someMethodHowIGetTheDataAssignedToTableCell
//nameLabel.text = name
}
}
How do I pass the model (in this case the string) assigned to the single TableCell over to the new ItemViewController?
Like this. Just add a return push segue from ItemViewController back to ItemsViewController. Name them less similar in the future :-)
import UIKit
class ItemsViewController: UITableViewController {
let items = [
"Bob",
"Joe",
]
#IBAction
func unwindToSegue(segue: UIStoryboardSegue) {
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Item", forIndexPath: indexPath) as UITableViewCell
cell.textLabel.text = items[indexPath.row]
return cell
}
//Passing details to detail VC
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let indexPath = self.tableView.indexPathForSelectedRow()
let theDestination = (segue.destinationViewController as ItemViewController)
theDestination.itemName = items[indexPath!.row]
}
}
import UIKit
class ItemViewController: UIViewController {
#IBOutlet weak var nameLabel: UILabel!
var itemName = ""
override func viewDidLoad() {
super.viewDidLoad()
nameLabel.text = itemName
}
}

Resources