Swift 3 My tableView is not populatinG? - uitableview

I am wondering why is my tableview not populating? I have a tableview inside my view controller and I have tried to follow the guide however, nothing seems to show up on my tableview.
here's my code
#IBOutlet weak var weatherTableView: UITableView!
var items: [String] = ["Sunny", "Cloudy", "Rainy"]
override func viewDidLoad() {
super.viewDidLoad()
self.weatherTableView.register(UITableViewCell.self, forCellReuseIdentifier: "WeatherCell")
// Do any additional setup after loading the view.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:UITableViewCell = self.weatherTableView.dequeueReusableCell(withIdentifier: "WeatherCell")! as UITableViewCell
cell.textLabel?.text = self.items[indexPath.row]
return cell
}
am I missing something here?
Thanks for your help in advance

Make sure you are assigning tableview delegate and datasource throw storyboard or add below line into your viewDidLoad() of your viewcontroller (UITableViewDelegate, UITableViewDataSource)
weatherTableView.delegate = self;
weatherTableView.dataSource = self

Related

Swift table view not showing contents

I wanted to give a go at swift, and looked at several tutorials. I tried to implement a TableView.
Here is my code :
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
var items: [String] = ["lol1", "lol2", "lol3"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1;
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "cell")!
cell.textLabel?.text = self.items[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
print("You selected cell #\(items[indexPath.row])!")
}
}
MY IBOutlet is connect to the tableview I inserted in the storyboard.
When I run it, I have a TableView, but it's missing contents.
From what I gathered through some (more or less outdated) tutorials, I shouldn't have anything more to do, what am I missing ?
Where are you set dataSource and Deleagte methods of TableView?
use this code
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.tableView.dataSource = self
self.tableView.delegate = self
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
2 possible reasons:
If the cell is designed as prototype cell you must not register the cell.
dataSource and delegate of the table view must be connected to the controller in Interface Builder or set in code.

TableView won't show data at all

Trying to crate a simple tableView that show data but it's not working. I checked and it's not even get into func tableView .. Any ideas what can be the problem?
import UIKit
class ViewController: UIViewController,UITableViewDelegate , UITableViewDataSource , UITextFieldDelegate {
let messageArray = ["First message ", "sec message " , "Third message"]
#IBOutlet weak var customTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
customTableView.delegate = self
customTableView.dataSource = self
customTableView.register(UINib(nibName: "customCell" , bundle: nil), forCellReuseIdentifier: "tableCell")
configureTableView()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell"
, for: indexPath)as! custom
cell.label0.text = messageArray[0]
cell.label1.text = messageArray[1]
cell.label2.text = messageArray[2]
cell.label3.text = "d"
customTableView.reloadData()
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func configureTableView() {
customTableView.rowHeight = UITableViewAutomaticDimension
customTableView.estimatedRowHeight = 120.0
}
}
Try deleting the reload data from cellForRowAt and implementing
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
I think you have a recursive reload becuase you have this in cellForRowAt
customTableView.reloadData()
so comment it and try
Register cell before set delegate and data source.
Remove customTableView.reloadData() in func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
Simply remove this line: customTableView.reloadData() from datasource method cellForRowAt.
You are basically asking the tableview to reload all of it's data while it is getting the first row from the datasource method. So it's an endless loop. It never gets to show any data.

How to put a tableView inside a section Header

I’m trying to put a tableView inside a collectionView sectionHeader. I tried adding a tableView to the header in storyboard and then setting its class to tableViewController.swift but that didn’t work.
(I’m using swift)
After trying a bit i did manage to get it working properly.
First i created a header as normal, then created an IBOutlet of the tableView.
Then I simply setup the header file like a tableViewController is setup, connected to the dataSource and delegate in awakeFromNib and done.
class HomeHeader: UICollectionReusableView, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var tableView: UITableView!
override func awakeFromNib() {
tableView.delegate = self
tableView.dataSource = self
//tableView.backgroundColor = UIColor.clear
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1 //number of sections
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5 //number of cells
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell
cell.textLabel?.text = "test"
// cell.backgroundColor = UIColor.clear
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
}

Table View doesn't show Data, why? (Swift 3)

I've made an app using a table view to persist data. The table view shows the elements of an array which is saved through UserDefaults. Since TableViewControllers look kind of ugly, I did it a bit different: I put a TableView into a normal ViewController.
My problem is: even though I did everything I could to get the TableView to show the data, it doesn't, and I don't know why. Could anyone help me?
Here's the code of the ViewController:
class ErfolgreicheChallenges: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet var myTableView: UITableView!
var data = [String]()
override func viewDidLoad() {
super.viewDidLoad()
myTableView.delegate = self
data.append("test")
data.append("lol")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
myTableView.dataSource = self
let cell = myTableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel!.text = data[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
}
Thanks to everyone for having a look! Have a great day
Replace viewDidLoad with
override func viewDidLoad() {
super.viewDidLoad()
myTableView.dataSource = self
myTableView.delegate = self
data.append("test")
data.append("lol")
myTableView.reloadData()
}
and replace numberOfRowsInSection with
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
and delete myTableView.dataSource = self in cellForRow.
If the code is crashing then myTableView is not connected in IB.
As mentioned in the comments, consider to use UITableViewController which got a table view instance with pre-connected data source and delegate.
You need to set the data source and data delegate in viewDidLoad, or somewhere similar.
You are setting the datasource inside cellForRowAt, but that will never be called!

Pass variable from one ViewController to another

I have an array: var initialArray. That appends every time the screen is tapped. This is done on ViewController 3
I have a table which displays the array in another view controller: ViewController2.
However the issue is: use of unresolved identifier: initialArray.
How can I get ViewController2 to recall variables from ViewController3?
class ViewController2: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tableView.delegate = self
tableView.dataSource = self
}
//How many rows
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return initialArray
//////use of unresolved identifier: initialArray
}
//What goes in each cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
print(indexPath.row)
cell.textLabel?.text = initialArray[indexPath.row]
//////use of unresolved identifier: initialArray
return cell
}

Resources