UITableViewCell not visible in UITableView - ios

I have a UITableView on the ViewController. Here is complete code
import UIKit
class UnitTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var unitTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.unitTable.register(UnitCell.self, forCellReuseIdentifier: "UnitCell")
self.unitTable.delegate = self
self.unitTable.dataSource = self
}
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 {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = unitTable.dequeueReusableCell(withIdentifier: "UnitCell", for: indexPath) as! UnitCell
cell.unitNameLbl?.text = "TEST"
return cell
}
}
But unfortunately my custom cell is not visible. I've put green background colour to be sure that constraints are right.
Here is my UnitCell code
import UIKit
class UnitCell: UITableViewCell {
#IBOutlet weak var unitNameLbl: UILabel?
#IBOutlet weak var unitNumberLbl: 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
}
}
What could be wrong if I can't see my custom cell?

try this following into viewDidLoad
unitTable.register(UINib(nibName: "yourCellClassName", bundle: nil), forCellReuseIdentifier: "UnitCell")

You can't directly use class name of cell because you'r using XIB of cell so you must need to register cell with nib name like below.
let nib = UINib(nibName: "YourTableViewCellNibName", bundle: nil)
self.unitTable.register(nib, forCellReuseIdentifier: "UnitCell")

change registering cell to table code as below.
override func viewDidLoad() {
super.viewDidLoad()
self.unitTable.register(UINib(nibName: "UnitTable", bundle: nil), forCellReuseIdentifier: "UnitCell") // nibName should be exact name of xib file of your custom cell.
self.unitTable.delegate = self
self.unitTable.dataSource = self
}

Related

Table View items are not showing

I am new to iOS development.
I'm trying to display a list of items in my app using UITableView, but the items are not showing:
My View Controller:
class HistoryView: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var tableView: UITableView!
let cellReuseIdentifier = "cell"
#IBOutlet weak var noDataLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(HistoryTableViewCell.self, forCellReuseIdentifier: "historyCell")
tableView.delegate = self
tableView.dataSource = self
self.noDataLabel.isHidden=true
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "historyCell") as! HistoryTableViewCell
cell.nickname?.text = "name"
return cell
}
}
My Table View Cell:
class HistoryTableViewCell: UITableViewCell {
#IBOutlet weak var nickname: 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
}
}
Any suggestions for what's going on?
condition 1:
if you are using prototype cells remove the line
tableView.register(HistoryTableViewCell.self, forCellReuseIdentifier:"historyCell")
and give cell identifier in the storyboard
condition 2:
if you are using xib file then register the cell like below:-
tableView.register(UINib(nibName: "HistoryTableViewCell", bundle: nil), forCellReuseIdentifier: "historyCell")
you need to register cell like this
tb.register(UINib(nibName: "HistoryTableViewCell", bundle: nil), forCellReuseIdentifier: "historyCell")
I saw #IBOutlet weak var nickname: UILabel!
in the class HistoryTableViewCell.
for table cells,
dequeueReusableCell and register, should called in pairs

UITableView always displays basic cell instead of custom cell in Swift

I spent hours of trying to fix this, but my simple app still displays the basic cell type instead of my prototype cell. I'm aware of using the identifier and registering after loading up the view, but it still displays the basic cells with just one label.
Here is my code so far:
My prototype is using this UITableViewCell:
class CoinTableViewCell: UITableViewCell {
#IBOutlet weak var coinIcon: UIImageView!
#IBOutlet weak var coinTitleLabel: UILabel!
#IBOutlet weak var holdings: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
UITableViewController:
class CoinTableViewController: UITableViewController {
var coins = ["Coin1","Coin2","Coin3"]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(CoinTableViewCell.self, forCellReuseIdentifier: "currency_cell")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return coins.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:CoinTableViewCell! = self.tableView.dequeueReusableCell(withIdentifier: "currency_cell", for: indexPath) as! CoinTableViewCell
let coinName = coins[indexPath.row]
cell.coinTitleLabel?.text = coinName
return cell!
}
}
I would be so grateful if someone is able the help me out with this!
You are creating your custom cell directly on the tableview in the storyboard, right ?
If this is the case then you don't need to register the cell in your viewDidLoad as the storyboard takes care of that. You just deque it and it's good to go.
If you register it manually you just override what the storyboard did and end up getting a regular cell as the cell gets instantiated from the code instead of getting instantiated from the storyboard.
Cheers

Custom cell content doesn't show Swift

I want to create a custom table view. I have an UITableViewController:
class MainListView: UITableViewController{
var invoceList: [InvoceObject] = []
#IBOutlet var tbMain: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(CustomCell.self, forCellReuseIdentifier: "cell")
tbMain.delegate = self
self.tbMain.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.invoceList.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
cell.lblBuyerName?.text = self.invoceList[indexPath.row].BUYER_NAME
cell.lblDate?.text = self.invoceList[indexPath.row].CREATED_AT
cell.lblPriceGross?.text = self.invoceList[indexPath.row].PRICE + " " + self.invoceList[indexPath.row].CURRENCY
cell.backgroundColor = UIColor(white: 1, alpha: 0.5)
return cell
}
}
And my UITableViewCell class:
class CustomCell: UITableViewCell {
#IBOutlet weak var lblBuyerName: UILabel!
#IBOutlet weak var lblDate: UILabel!
#IBOutlet weak var lblPriceGross: 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
}
}
After build I get an empty cells. I have a number of rows like invoceList.count, but there is no labels and it looks as empty table. What I'm doing wrong?
Since you are using UITableViewController there is an implicit tableView property with connected data source and delegate. You should use that to avoid confusion.
And delete the line self.tableView.register... if you are using prototype cells.
If the cell's contents are defined in a separate xib file, you need to register that, not the class - if you only register the class, the actual labels are not being created because the class itself does not create them.
//Replacing CustomCellNibName with the name of your xib file
tableView.register(UINib.init(nibName: "CustomCellNibName", bundle: nil), forCellReuseIdentifier: "Cell")
If the cell is being created in the storyboard as a prototype cell, you do not need to register it, but just associate the class with your cell in the storyboard file (under the identity inspector tab, Custom Class), and fill in the name you want to use (in this case, "Cell") under the Attributes inspector field Identifier.

How can I display multiple string values to multiple labels in a custom TableView Cell in swift ios?

var leadername = ["1","2","3","4"]
var districts = ["Delhi","Kerala"]
override func viewDidLoad() {
leadTableSetup()
super.viewDidLoad()
}
func leadTableSetup(){
LeadTableView.delegate = self
LeadTableView.dataSource = self
self.LeadTableView.register(UINib(nibName: "LeaderBoardTableViewCell", bundle: nil), forCellReuseIdentifier: "leadCell")
}
func numberOfSections(in tableView: UITableView) -> Int {
return 5
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 14
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "leadCell") as! LeaderBoardTableViewCell
// Set text from the data model
cell.areaLbl.text = districts[indexPath.row]
cell.leaderNameLbl.text = leadername[indexPath.row]
return cell
}
I have declared two strings and I need to display these strings in the labels in my custom collection view cell that I have created. How can I achieve this? I need to display "leadername" string in one label and "districts" label in another label.
Go with this demo, Shared Demo
After the demo, If you still face any problem then let me know.
Now Listen Here
I think you need output something like this,
Follow the steps: -
Create a new viewcontroller(says, CustomTableVC) in your storyboard and one UITableView(give constraints and delegate to its own class), take outlet of UItableView (says, tblMyCustom)
Now press CLT+N for new file and do like this below image, Subclass - UItableViewCell and also tick on XIB option.
Open our xib file, add new UIView (says myView, as you see highted in below image), in this myView add two labels
Now take outlet of these two labels in its customCell class
class CustomTableCell: UITableViewCell {
#IBOutlet var lblLeaderNo: UILabel!
#IBOutlet var lblDistict: 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
}
}
Now back to your Viewcontroller Class
import UIKit
class CustomTableVC: UIViewController , UITableViewDelegate, UITableViewDataSource{
#IBOutlet var tblMyCustom: UITableView!
var leaderno : [String]!
var distict : [String]!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.tblMyCustom.register(UINib(nibName: "CustomTableCell", bundle: nil), forCellReuseIdentifier: "customCell")
self.leaderno = ["1", "2", "3", "4"]
self.distict = ["Delhi","Kerala", "Haryana", "Punjab"]
// above both array must have same count otherwise, label text in null
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return leaderno.count;
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
var customCell: CustomTableCell! = tableView.dequeueReusableCell(withIdentifier: "customCell") as? CustomTableCell
customCell.lblLeaderNo.text = self.leaderno[indexPath.row]
customCell.lblDistict.text = self.distict[indexPath.row]
return customCell
}
}
above all is code of VC, it is not getting settle down here in single code format, I dont know why.
Now, follow these steps you get output as i show you image in starting of the procedure.

Loading a Custom UITableViewCell from nib to a UIViewController in Swift

I'm currently working on an exercise swift program, one that requires CoreData results to be displayed on a table.
I've structured my app such that the storyboard itself doesn't contain any UI elements, only views (with an accompanying UIViewController class), which then loads custom nibs / xibs (also accompanied by a UIView Subclass). In this case, the xib contains a UITableView, and a separate xib contains the cell.
TableView Class:
import UIKit
protocol SkillsViewDelegate{
}
class SkillsView: UIView {
var delegate : SkillsViewDelegate!
#IBOutlet weak var skillsTable: UITableView!
}
TableView Controller:
import UIKit
import CoreData
class SkillsViewController: UIViewController, SkillsViewDelegate, UITableViewDataSource, UITableViewDelegate {
var displaySkillsView : SkillsView!
var displaySkillsCell : SkillViewCell!
let textCellIdentifier = "skillViewCell"
override func viewDidLoad() {
super.viewDidLoad()
self.displaySkillsView = UIView.loadFromNibNamed("SkillsView") as! SkillsView
self.displaySkillsView.delegate = self
self.view = self.displaySkillsView
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//MARK : -Table view delegates
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath)
return cell
}
}
TableCellView Class:
import UIKit
protocol SkillsViewCellDelegate{
}
class SkillViewCell: UITableViewCell {
var delegate : SkillsViewCellDelegate!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
#IBOutlet weak var skillName: UILabel!
#IBOutlet weak var skillRank: UILabel!
}
I can't figure out how to call the cell xib into the controller. I would guess it's something like this problem, but the difference is the guy is using a UITableViewController. I tried adding that as well, but I get a Multiple inheritance from classes 'UIViewController' and 'UITableViewController' for my troubles.
What I Tried:
I tried adding UITableViewController up top, but I get a Multiple inheritance from classes 'UIViewController' and 'UITableViewController' for my troubles.
Any suggestions?
You can just register your SkillsViewCell in your SkillsViewController
Obj C
-(void)viewDidLoad {
[super viewDidLoad];
UINib *cellNib = [UINib nibWithNibName:#"NibName" bundle:nil];
[self.skillsTable registerNib:cellNib forCellReuseIdentifier:textCellIdentifier];
}
Swift 2
override func viewDidLoad() {
super.viewDidLoad()
let nibName = UINib(nibName: "NibName", bundle:nil)
self.skillsTable.registerNib(nibName, forCellReuseIdentifier: textCellIdentifier)
}
Swift 4
override func viewDidLoad() {
super.viewDidLoad()
let nibName = UINib(nibName: "NibName", bundle:nil)
self.skillsTable.register(nibName, forCellReuseIdentifier: textCellIdentifier)
}
Then only inherit from UIViewController not UITableViewController
To call methods specific to your custom cell
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as! SkillViewCell
cell.skillName.text = "Some text"
cell.skillRank.text = "Some text"
return cell
}
I think you will need to use
func registerNib(_ nib: UINib?, forCellReuseIdentifier identifier: String)
in viewDidLoad
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/#//apple_ref/occ/instm/UITableView/registerNib:forCellReuseIdentifier:

Resources