Rohit Singh
Rohit Singh

Reputation: 18202

TableViewCell does not get data in Swift

I am using a Custom TableViewCell in TableViewController. Even the data is getting populated but my TableViewCell does not display it.

Here is my Custom TableViewCell Code

class PlaceItemCellCustom: UITableViewCell {

   @IBOutlet weak var placeFace: UILabel?
   @IBOutlet weak var placeName: UILabel?

   override func awakeFromNib() {
      super.awakeFromNib()
   } 

}

Here is my TableViewContoller

class PlaceListViewController: UITableViewController {

   override func viewDidLoad() {
      super.viewDidLoad()
      loadListFromSource()
   }
  
   // Other code

   override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
   }

   override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       return places.count
   }

   override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
       return 100
   }

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

       let place = places[indexPath.row]
       // I am getting the data 
       print(place.placeName?.description ?? " " )
    
       let customCell = tableView.dequeueReusableCell(withIdentifier: "PlaceListIdentifier", for: indexPath) as! PlaceItemCellCustom
    
       customCell.placeName?.text = place.placeName
   
       return customCell
   }

}

What am I missing here? I am new to IOS app development. Found similar questions but did not help

Edit: It was working fine for the default TableViewCell.

But when I changed it to custom TableViewCell it does not work. I have set the class name in the storyboard as well.

Here is my Storyboard

enter image description here

Here is the output

enter image description here

Upvotes: 0

Views: 75

Answers (2)

Amit
Amit

Reputation: 4886

As much as I can understand you must have forgotten to connect the IBOutlet of placeName in PlaceItemCellCustom:

enter image description here

In your PlaceItemCellCustom keep placeName as :

@IBOutlet weak var placeName: UILabel!

and in cellForRowAt:

customCell.placeName.text = place.placeName

in this way if you forgot to connect segue Xcode will throw error as :

Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

You can read more about it here :

Upvotes: 1

B.Dhruvin
B.Dhruvin

Reputation: 31

You need using this method for get data.

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return places.count
} 

Upvotes: 0

Related Questions