UITableView Displaying Incorrectly - swift

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.

Related

Table View, View Controller, Table Cell nil error

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")

How to fix 'tableview cellforrowat not called?'

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

Cell from Xib with Swift 3

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")

Can't resolve SIGABRT error - Times Table App with UITableView

I am trying to build a times tables app with a slider and a UITableView, but it gives me the SIGABRT error. I have tried relinking every outlet and action as well as redoing it from scratch, but it still won't seem to work.
Since I cannot paste it in here correctly formatted, I put it on pastebin.com
Debug code: http://pastebin.com/cBaALXWq
ViewController.swift
import UIKit
class ViewController: UIViewController, UITableViewDelegate {
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.
}
#IBOutlet weak var sliderValue: UISlider!
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 50
}
#IBAction func sliderMoved(sender: AnyObject) {
var timesTableIndex: Int = Int(sender.value)!
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
cell.textLabel?.text = String(timesTableIndex*indexPath.row+1)
return cell
}
}
}
AppDelegate.swift; gives error in line 4
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
EDIT
#IBOutlet weak var sliderValue: UISlider!
var timesTableIndex: Int?
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 50
}
#IBAction func sliderMoved(sender: AnyObject) {
timesTableIndex = Int(sliderValue.value * 50)
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
cell.textLabel.text = String(timesTableIndex*(1+indexPath.row))
return cell
}
I looks like you've added cellForRowAtIndex: inside #IBAction method. What you need to do is to take it out from the method. In addition you need set the timesTableIndex variable as global so that it can be accessed in cellForRowAtIndexPath: and call the reloadData method on tableView when you call sliderMoved: method.
var timesTableIndex: Int = 0 // Now global variable
#IBAction func sliderMoved(sender: AnyObject) {
timesTableIndex = Int(sender.value)!
self.tableView.reloadData()
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
cell.textLabel?.text = "\(timeTableIndex * indexPath.row + 1)"
return cell
}
Also it seems that you are not using dequeueReusableCellWithIdentifier method on your table view, so I've updated the code to use it. Not that you need to set the Cell reuse identifier in your storyboard.
This should be your tableview code if you have created tableview in the storyboard
#IBOutlet weak var sliderValue: UISlider!
#IBOutlet weak var tableview: UITableView!
var timesTableIndex: Int = 0
#IBAction func sliderMoved(sender: AnyObject) {
timesTableIndex = Int(sliderValue.value * 50)
tableview.reloadData()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return timesTableIndex
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.textLabel!.text = String(1*(1+indexPath.row))
return cell
}
If you have created your cell in the storyboard set the CellReuseIdentifier as 'cell' in the storyboard else you can register the class in the like
override func viewDidLoad() {
super.viewDidLoad()
//tableView is IBOutlet instance of UITableview
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "test")
}
Also please check you have set the tableview delegate/datasource in the storyboard tableview object
I've managed to solve the problem. I accidentally put the function which sets up the prototype cell outside the ViewController class, that explains everything. Thank you for helping me!

How to add data to UITableViewController

I have a problem with some code in my project. I want to set a UITableViewController with a prototype cell with three labels. When i click on the add button appears an add controller with three text fields to add some data. I want that this data appear in the cell's labels of the UITableViewController but i don't understand how to set code for this. Can you help me?
Wow, you really don't know much about tableViews. But its ok. Try to use this code for your table view...
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var tableview: UITableView!
let sampleData:[String] = ["data1", "data2", "data3", "data4", "data5"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//table view datasource and delegate
self.tableview.dataSource = self
self.tableview.delegate = self
self.tableView.tableFooterView = UIView(frame: CGRectZero)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//returns number of rows in table view
return self.sampleData.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = self.tableview.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
let label:UILabel = cell.viewWithTag(1) as UILabel
label.text = sampleData[indexPath.row]
return cell
}
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
//user selected a row
//print the table view row number
println(indexPath.row)
}
}
Let me know if you have any questions.