I'm trying to create a swift program with a main ViewController, and a secondary VC that controls a pop up that overlays the screen. In this pop up, you can enter in login credentials, which will display in a tableView in the main ViewController.
So far I'm able to save the entered values in the secondary VC to core data, but I'm not able to reloadData() for the tableView in the first VC, because it just cant see the reloadData() function (must not be a public function? Not really sure, as I'm new to swift).
Could someone help me figure out the best way to go about doing this?
Main ViewController code:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var tableView: UITableView!
var account = [Account]()
#IBAction func onAddAccount(_ sender: Any) {
self.tableView.reloadData()
print("RELOADED")
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func onBlank() {
self.tableView.reloadData()
}
}
extension ViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return account.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: nil)
cell.textLabel?.text = account[indexPath.row].email
cell.detailTextLabel?.text = account[indexPath.row].password
return cell
}
}
I am relatively new to swift myself. But I think what you are looking for is a Delegate will tell the main controller that the data has been updated and what it's been updated too. It also seems you might need a datasource? So that you can implement model, view, controller architecture.
Related
I've created a ViewController with my Storyboard linked to it. I've done this before and it has worked fine. This time tho, when I use almost the exact same code for the table view and the table view cell, it returns a nil error when running. I am not sure what the reason exactly is.
//
// IngView1.swift
// Fake
//
// Created by Ian Dong on 11/16/20.
//
import UIKit
class IngView1: UIViewController, UITableViewDataSource {
#IBOutlet weak var display: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
display.dataSource = self
}
var data = ["Oil 5","Beef 3","Chicken 5","Potato 4","Cheese 3","Fish 3","Cabage 4"]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "IngReUse")! // Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
let text = data[indexPath.row]
cell.textLabel?.text = text
return cell
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
}
Are you sure you properly set prototype cell's identifier? You can do this in the storyboard.
Or if you're not using prototype cell, you can register it in code
display.register(UITableViewCell.self, forCellReuseIdentifier: "IngReUse")
I am new to swift and I have this app in mind that basically needs to update all the cells based on the data received from one of the cells.
I am using dynamic cells and each one has a textfield inside.
TableViewController.swift:
import UIKit
class MyCells: UITableViewCell{
#IBOutlet weak var value_textfield: UITextField!
}
class TableViewController: UITableViewController {
var units_length: [String] = ["Centimeter", "Meter", "Foot"]
override func viewDidLoad() {
super.viewDidLoad()
units_length = units_length.sorted()
self.tableView.keyboardDismissMode = .onDrag
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return units_length.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = units_length[indexPath.row]
cell.textLabel?.textColor = UIColor.white
cell.textLabel?.font = UIFont(name:"DINAlternate-Bold", size:30)
return cell
}
override func viewWillAppear(_ animated: Bool){
tableView.rowHeight = 100
}
}
Which function should I use to access all the cells with different identifiers and simultaneously update all the textfield inside each cell?
I do have access to the textfield by having another class called MyCells, how should I implement it?
(I will not include the base ViewController class here as there's nothing inside)
Thanks!
Normally you use the function you already showed: cellForRowAt. You update your model data, tell the table to reloadData, cellForRowAt is called for all visible cells, and their values become the new values.
However if you are updating after every character, I would set up a notification broadcast situation and update all visible text fields and the model together without a reload. Just make sure the model and the table stay in sync in case the user scrolls and cellForRow is called.
I am trying to use a UITableView by implementing the code I saw on many tutorials but it doesn't work correctly when I run it. What am I doing wrong? I added a cell to the view and added a corresponding identifier. My storyboard look like that
and code is the following:
import UIKit
import Firebase
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var ref: DatabaseReference?
var data = ["m","2","3"]
#IBOutlet weak var tableV: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableV.delegate = self
tableV.dataSource = self
// 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 tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableV.dequeueReusableCell(withIdentifier: "PostCell")
cell?.textLabel?.text = data[indexPath.row]
return cell!
}
}
And
what it looks like.
Please help me to solve this problem.
You should use this way to correct errors.
change this:
let cell = tableV.dequeueReusableCell(withIdentifier: "PostCell")
with:
let cell = tableV.dequeueReusableCell(withIdentifier: "PostCell", for: indexPath)
and you need to check cell identifier.
I already read and saw some videos about this subject but i can't get this to work. Im trying to a cell from a xib instead of the storyboard.
This is my ViewController (where i have the Table View).
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet 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 tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CellTableView
cell.test.text = "A";
return cell;
}
}
This is my CellTableView (the class where i have my Cell):
import UIKit
class CellTableView: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
#IBOutlet var teste: UILabel!
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
I always used the Table View Cell inside the Table View on the StoryBoard but i want to try a XIB approach now.
I added the cell Identifier on the cell XIB to call it with the dequeueReusableCell method.
What i could be doing wrong? I tried to Outlet the dataSource and the delegate of the Table View but i got an error (and i think its not the right steps).
You need to register your nib object.
In viewDidLoad():
let nib = UINib(nibName: "nameOfYourNibFile", bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "yourIdentifier")
You also need to return the number of sections:
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
First of all, You have to register your nib object.
yourTable?.register(UINib(nibName: "yourCustomCell", bundle: nil), forCellReuseIdentifier: "cellIdentifier")
This is my history view, I have a tableview inside view controller. But for some reason i can't output anything, I am trying to hardcode it and its not displaying anything at all. One thing i know is i need to make tableView functions override and if i do that i get error.
import UIKit
class History: UIViewController, UITableViewDataSource, UITableViewDelegate
{
#IBOutlet var tableView: UITableView!
override func viewDidLoad()
{
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "newBackground.jpg")!)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("historyCell", forIndexPath: indexPath) as! historyCell
cell.durationLabel.text = "98"
return cell
}
override func viewWillAppear(animated: Bool)
{
super.viewWillAppear(animated)
}
}
heres my class for the cell prototype, and i made sure that the identifier is also "historyCell"
public class historyCell: UITableViewCell{
#IBOutlet var durationLabel: UILabel!
#IBOutlet var kicksLabel: UILabel!
}
How did you connect your prototype cell's controls to the IBOutlets in you historyCell class ? I didn't know that was possible when you're not using a UITableviewController. I've been using tags all this time.
Unless there's something new that I wasn't aware of, your assignment to cell.durationLabel.text should cause unwrapping of a nil (is that the error you're getting ?)