Swift Custom Uitableviewcell Not Displaying Data

Swift Custom UITableViewCell not displaying data from array

Do following :

  1. Check your typo for cell and class name
  2. Check whether delegate and datasource of UITableview is properly binded or not
  3. If your using xib for TableViewCell then register your xib in VDL

Note:
1. Make a space between as and customCell like this as! CustomCell. And its better for iOS if you declare
variable cell like this let cell : CustomCell = .......... because on this case x-code know your variable type. [may be sometimes it dos not create conflicts but it is not in a good practice of coding].

2 .Possibly change your tableview outlet name @IBOutlet var tableView: UITableView! to any other name because tableView is already in build name taken by the iOs if you use same variable then it overrides and caused conflict for the x-code.

edit

Instead of self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as!CustomCell remove self from the line because your are not dequeueing your tableView object, here you just need to dequeue your parameter of the cellForRow method.

Use below code :

let cell : CustomCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell

Solution

Use this below demo image to bind your delegates and datasource from the storyboard,I have added output after doing this, check.

Demo Image

Sample Image

Output from your project:

Sample Image

Swift Custom UITableViewCell not displaying data

When you make a custom cell in the storyboard, don't register the class (or anything else). Just be sure to give the cell the same identifier in the storyboard that you pass to dequeueReusableCellWithIdentifier:forIndexPath:.

Custom UITableViewCell does not display correctly

Changing the class of the content view of a UITableViewCell may result in unexpected behaviours, as the content view is the default superview of the content and UIKit might process some configuration on it. (which I can't find a clear instruction from the documentation)

Reference from Apple's Documentation

The content view of a UITableViewCell object is the default superview for content that the cell displays. If you want to customize cells by simply adding additional views, you should add them to the content view so they position appropriately as the cell transitions in to and out of editing mode.

So a workaround of adding a custom subview, is to add it as a subview of the content view, and leave the content view's properties untouched. Then you can wrap all your content inside the your custom view.

Avoid changing the class of content view

Why is my custom UITableViewCell showing not displaying properly?

You have the table view controller and prototype cell in the storyboard. But you are creating the view controller programmatically

let v = ControlSettingsView()

So the prototype cell properties will be nil. Create the view controller instance using storyboard identifier

Make sure you've set storyboard identifier ControlSettingsView for the view controller

if let vc = self.storyboard?.instantiateViewController(withIdentifier: "ControlSettingsView") as? ControlSettingsView {
self.navigationController?.pushViewController(vc, animated: true)
}

And don't use register(_:forCellReuseIdentifier:) method if you have the tableview cell in the storyboard.

custom UITableViewCell - not showing

do like

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell: SearchTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("reuseIdentifier") as! SearchTableViewCell

cell.routeNumberLbl.text = transportData[indexPath.row].routeNumber
cell.routeDescriptionLbl.text = transportData[indexPath.row].description
cell.distanceLbl.text = transportData[indexPath.row].dist

return cell
}

updated answer

override func viewDidLoad() {
super.viewDidLoad()
self.lvMain.delegate = self // invoke data source and delegate on current class
self.lvMain.dataSource = self
fillMockup()

}

func fillMockup(){
//just some mockup data to test if it's working
transportData.append(RouteModel(type: RouteModel.TYPE_BUS,routeNumber: "44", description: "Ярославль - Сан Тропе",dist: "200 m"))
transportData.append(RouteModel(type: RouteModel.TYPE_TRAM,routeNumber: "11", description: "Ярославль - Сан Тропе",dist: "400 m"))
transportData.append(RouteModel(type: RouteModel.TYPE_R_BUS,routeNumber: "1", description: "Ярославль Главный - Жопа Мира",dist: "1.4 км"))
transportData.append(RouteModel(type: RouteModel.TYPE_TROLLEY,routeNumber: "24", description: "Ярославль - Малые Гребеня",dist: "1.4 км"))
transportData.append(RouteModel(type: RouteModel.TYPE_BUS,routeNumber: "43", description: "Ярославль - Малые Гребеня",dist: "1.4 км"))
transportData.append(RouteModel(type: RouteModel.TYPE_BUS,routeNumber: "43а", description: "Ярославль - Малые Гребеня",dist: "1.4 км"))
transportData.append(RouteModel(type: RouteModel.TYPE_BUS,routeNumber: "85б", description: "Ярославль - Брагино",dist: "1.4 км"))
transportData.append(RouteModel(type: RouteModel.TYPE_BUS,routeNumber: "38", description: "Ярославль - Брагино",dist: "2.4 км"))

self.lvMain.reloadData() //reload the table when does loaded perfectly
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell: SearchTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("reuseIdentifier") as! SearchTableViewCell

cell.routeNumberLbl.text = transportData[indexPath.row].routeNumber
cell.routeDescriptionLbl.text = transportData[indexPath.row].description
cell.distanceLbl.text = transportData[indexPath.row].dist

return cell
}

for additional example see this

Custom Cell in table view not showing?

If you create the custom cell from Nib file, you should define the cell in this way:

tableView.register(UINib(nibName: "HoursTableViewCell", bundle: nil), forCellReuseIdentifier: cellReuseIdentifier)

another point:

If you are using nib file for your custom cell and set IBOutlet, there is no need to define your elements as

@IBOutlet var entryTitleLabel: UILabel! = {
let label = UILabel()
return label
}()

you just need to set @IBOutlet var entryTitleLabel: UILabel!

UITableView Not displaying data AND not adding data

You are missing two lines in viewDidLoad():

override func viewDidLoad() {  
super.viewDidLoad()

tableView.dataSource = self // This class is the tableview's data source
tableView.delegate = self // This class is the tableview's delegate
}

Custom uitableview cell not showing all text labels

You are using the standard UITableViewCell and you assign all three values to the same label.

You have to cast the cell to the custom cell and assign the values to the custom labels

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

// let use a hack for now, we actually need to dequeue our cells for memory efficiency
// let cell = UITableViewCell(style: .Subtitle, reuseIdentifier: cellId)

let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! UserCell

let user = users[indexPath.row]
cell.nameLabel?.text = user.date_clasname
cell.teacherLabel?.text = user.teacher
cell.roomLabel?.text = user.room_number

return cell
}

Replace nameLabel, teacherLabel and roomLabel with the real property names.

And please conform to the naming convention and name variables lowerCamelCased for example dateClasname and roomNumber



Related Topics



Leave a reply



Submit