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.
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'm setting up a tableview with customtableview cell, but when i run the app, the data is not shown.
I already check all the connection, identifier, class name, etc
import UIKit
class ViewController: UITableViewController {
#IBOutlet var testingTableView: UITableView!
var data = ["Indomie","Kacang Tanah","Soya"]
override func viewDidLoad() {
super.viewDidLoad()
testingTableView.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: "customCell")
// Do any additional setup after loading the view.
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = testingTableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! customTableViewCell
cell.customLabel.text = data[indexPath.row]
return cell
}
override func numberOfSections(in tableView: UITableView) -> Int {
return data.count
}
}
This is my code for the table view, it's should call the cellforrowat but it's not
You are missing tableView(_:numberOfRowsInSection:) implementation.
You have number of data.count section, but each section has 0 row.
Try replacing numberOfSections(in:) to tableView(_:numberOfRowsInSection:)
I guess you need additional code as below-
testingTableView.delegate = self
testingTableView.datasource = self
testingTableView.reloadData() // I think you missed it!
1). Add this line
#IBOutlet var testingTableView: UITableView! {
didSet {
testingTableView.delegate = self
testingTableView.dataSource = self
}
}
2). You also can add this feature via Stroyboard. Click on your table and using right click drag the pointer on ViewController and add delegate and dataSource.
And i think you missed
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 0 //As per your need.
}
You are using the wrong API, numberOfSections returns the number of sections but you have to return the number of rows
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { {
return data.count
}
There is no need to override numberOfRowsInSection, the default value is 1.
And delete
#IBOutlet var testingTableView: UITableView!
There is an implicit tableView property of UITableViewController
I am new to programming and currently working on a newsfeed like app. I had a normal Table view up and running fine, but want to try it now with a costume cell type. So I created one and thought connecting the labels the usual way would be perfectly fine, but I was wrong. So I am wondering how I can let my Text label connect to my view controller, so I can use my custom cell.
class ViewController: BaseViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet var newsfeedTableView: UITableView!
var ref: DatabaseReference!
var posts = [String]()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (posts.count)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "cell")
//here is where I need the custom label to get the posts
cell.textLabel?.text = posts[indexPath.row]
cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 18.0)
return cell
}
}
Create subclass of UITableViewCell and connect IBOutlets to this class
class YourCell: UITableViewCell {
#IBOutlet weak var customLabel: UILabel!
...
}
don't forget to set class of your prototype cell in storyboard:
then in cellForRowAt data source method downcast your dequeued cell as YourCell
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! YourCell
then you have access to YourCell outlets
cell.customLabel.text = "SomeText"
...
I'm assuming that you are using Storyboard.
First of all, you should understand that there is little difference when you use own custom table cell. In that case, in the method "cellForRowAtIndexPath", after dequeue your cell, you just have to typecast table cell like 'as! YourCustomTableCellClass'. After this line, you can access each property of this class.
First, design your table cell on Storyboard whatever you want.
Now, make a subclass of UITableViewCell and assign this class to your prototype custom cell which you have designed on Storyboard. Also, don't forget to set "reuse identifier in Storyboard table cell.
Then connect your outlets with custom cell class from Storyboard.
Now you can use code like this:
class YourTableCellClass: UITableViewCell {
// I'm using these outlets as a sample, but you have to connect them from Storyboard.
var leftTextLabel: UILabel!
var rightTextLabel: UILabel!
}
class YourTableController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: - TableView Delegate & DataSource
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1 // return your number of rows here...
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100 // return the height for the row here.....or you can set height from Storyboard if you have fix height of all rows.
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifier", for: indexPath) as! YourTableCellClass
cell.leftTextLabel.text = "Text" // Set text here....
cell.rightTextLabel.text = "Text" // Set text here....
return cell
}
}
When I create a tableview, I cannot click on any of the items I put on the tableview. I was wondering how can I create a tableview that has every item clickable and when the user clicks on an item( say a city name, for example) it redirects the user to a different viewcontroller. ( for example if there are 22 clickable items in the tableview, there will be a total of 22 new different viewcontrollers )
Thank you very much in advance!
There are three major functions that a UITableViewDataSource must contain for the table view to work properly with user interaction (Pressing each row, for example). These functions are:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
The function you are looking to use is the third one. It is called when a user selects a certain row by tapping it on screen. You can find out the row index with 'indexPath'.
If you would like to go to 22 different view controllers, you need to create a manual segue between each one and label them accordingly. Then, you would want to call each individual segue depending on which row was selected inside of that third function! You can call a segue with an identifier with the performSegue() function.
Note that the class that contains these functions must be of type UITableViewDataSource, and you should tell the table view that it is the data source in the ViewDidLoad() function like so:
tableView.dataSource = self
Here is what a simple code can look like:
import UIKit
class viewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
var identifiers = [String]()
override func viewDidLoad() {
// fill your identifiers here
tableView.delegate = self
tableView.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return 22
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "yourCellIdentifier") as! yourCell
// fill your cell's data in here
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
// here you can use someThing like an array of your segue identifiers
self.performSegue(withIdentifier: identifiers[indexPath.row], sender: self)
//Or you can just implement a switch with every case doing what you want that cell to do which i don't recommend if you have 22 rows
}
}
Currently I have a tableview that loads cells when I scroll down (scrollview). Is it possible to load and populate all cells on viewDidLoad. I would like to assign data to a cell before it can be viewed. I have tried using self.tableView.reloadData() but not successful.
If you don't want to use UITableView's cell reusing concept, then create all the UITableViewCells beforehand in viewDidLoad and store them in an array.
Example:
class ViewController: UIViewController, UITableViewDataSource
{
var arr = [UITableViewCell]()
override func viewDidLoad()
{
super.viewDidLoad()
//Create your custom cells here and add them to array
let cell1 = UITableViewCell()
let cell2 = UITableViewCell()
arr.append(cell1)
arr.append(cell2)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return arr.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
return arr[indexPath.row]
}
}