TableView cellForRowAtIndexPath is never called (iOS, Swift) - ios

I have a problem with creating a table view in UITableViewController. I have tried everything I have found, reuseIdentifier is set correctly, number of rows returns non-zero number and height is set in storyboard. Only the cellForRowAtIndexPath is not called, other table functions runs correctly.
Here is the code:
TableViewController:
import UIKit
import Photos
class CropTableViewController: UITableViewController {
var photos = [Photo]()
var photoAssets = [PHAsset]()
override func viewDidLoad() {
super.viewDidLoad()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return photos.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! CropTableViewCell
let photo = photos[indexPath.row]
cell.photoImageView.image = photo.photo
return cell
}
}
TableViewCell:
import UIKit
class CropTableViewCell: UITableViewCell {
#IBOutlet weak var photoImageView: UIImageView!
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
}
}

The photos array is empty, so photos.count is zero, so numberOfRowsInSection returns 0, so the runtime knows that there are no cells and that there is no need to call cellForRowAtIndexPath.

Related

ScrollView delegate not called inside custom tableviewCell class

I'm using UITableViewCell custom class
Using Scrollview for horizontal scroll inside the tableviewcell
Write Tableview delegates in UIViewcontroller
Writing scrollview delegate in UITableview cell class
Delegate method not called.
My Code:
class ViewController: UIViewController {
#IBOutlet weak var matchesTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
matchesTableView.delegate = self
matchesTableView.dataSource = self
tableViewSetup()
}
func tableViewSetup() {
matchesTableView.rowHeight = UITableView.automaticDimension
matchesTableView.estimatedRowHeight = self.view.bounds.height
matchesTableView.tableFooterView = UIView()
matchesTableView.separatorStyle = UITableViewCell.SeparatorStyle.none
}
}
// MARK: UITableviewdelegate
extension ViewController : UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return self.view.bounds.height
}
}
UITableView Cell:
import UIKit
class TableViewCell: UITableViewCell, UIScrollViewDelegate {
#IBOutlet weak var bannerScrollView: UIScrollView!
#IBOutlet weak var matchesPageControl: UIPageControl!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code+
self.bannerScrollView.delegate = self
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let pageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width)
matchesPageControl.currentPage = Int(pageNumber)
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}

How to load data into table view

How do I load data into my UITableView? Can anyone please kindly help me with this issue:
why not load to cellForRowAt
pPlandDetailId ==>Optional(1)
Driver ==> Driver Name:Boonma,Pongpat
carRegistrtation ==> Car Registrtation:60-7620
StoreLocation ==> ["S.Surathani"]
pDepartureDateTime ==> ["18/01/2019 20:00"]
import UIKit
class StoreListMainViewController: UIViewController,UITableViewDelegate, UITableViewDataSource{
// TableView
#IBOutlet weak var tableView: UITableView!
var StoreLocation: [String] = []
var getDepartureTime: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
createTableiew()
}//Main Method
// TableView
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return StoreLocation.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? cellTableViewCell
cell!.Location.text = StoreLocation[indexPath.row]
cell!.DepartureTime.text = getDepartureTime[indexPath.row]
print("cell!.Location.text ==>\(String(describing: cell!.Location.text) )")
return cell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = storyboard?.instantiateViewController(withIdentifier: "gotoTempMonitorStoreList") as? StoreListTempMonitorViewController
self.navigationController?.pushViewController(vc!, animated: true)
}
// Create UI Table view
func createTableiew() -> Void {
tableView.delegate = self
tableView.dataSource = self
}//createTableiew
import UIKit
class cellTableViewCell: UITableViewCell {
#IBOutlet weak var Location: UILabel!
#IBOutlet weak var DepartureTime: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
reload tableview tableView.reload() where you have received data.
I suggest you to first fetch the data and later try to reload your table view.
Once you set the array to table values automatically they will get displayed in table provided you code object's properties to cell's Outlet values.
Since you are not using a UITableViewController you need to set a delegate and a dataSource to your tableView object.
Looks something like this:
override func viewDidLoad() {
tableView.dataSource = self
tableView.delegate = self
}
That's it. Enjoy

UITableView - Not showing

I have set up tableview many times but don't why is not it showing anything. I have added a tableview on my storyboard file, added reference on ViewController file and set datasource and delegate on storyboard. on viewDidLoad registered the cell and finally added reusableIdentifier on TableViewCell. Here is my code with details configuration :
import UIKit
struct ViewControllerConstants {
static let kCellIdentifier = "TableViewCell"
static let kNibName = "TableViewCell"
}
class ViewController: UIViewController {
var cellImageNames : [String] = ["image1","image2","image3","image4","image5","image6"]
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.tableView.register(UINib.init(nibName: ViewControllerConstants.kNibName, bundle: nil), forCellReuseIdentifier: ViewControllerConstants.kCellIdentifier)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(_ animated: Bool) {
self.tableView.reloadData()
}
}
extension ViewController : UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellImageNames.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: ViewControllerConstants.kCellIdentifier, for: indexPath) as? TableViewCell {
cell.contentImageView.image = UIImage.init(named: cellImageNames[indexPath.row])
return cell
}
return UITableViewCell.init()
}
}
extension ViewController : UITableViewDelegate {
}
Try to remove identifiers for cells in storyboard.
Problem: Cell height was not defined on code. So it was creating cell with 0 height value .
Solution: Need to specify height of each row as below code:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}

I can't make outlet in uitableviewcell

I don't want to make my code messy so I made a separate class for table view cell but when I tried to make outlet of my UIImageView, it's showing error
import UIKit
class TableViewCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
#IBOutlet var image: UIImageView!
}
// code of my tableview
import UIKit
class ViewController: UIViewController, UITableViewDelegate,UITableViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
var array = ["apple","blue_graps","cherry","grape","Pineapple","pomegranate","Strawberry"]
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.imageView?.image = UIImage(named: array[indexPath.row])
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
}
Change the variable image to some other name, e.g. myImage or icon.
There is a deprecated property in UITableViewCell called image and you cannot redeclare it. (It was deprecated in iOS 3 already but it's still there).

Custom TableViewCell Swift

Here is the code from my custom cell class:
import UIKit
class CustomOrderTableViewCell: UITableViewCell {
#IBOutlet var MealName: UILabel!
#IBOutlet var MealPrice: UILabel!
#IBOutlet var MealDescription: 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
}
#IBAction func deleteMeal(sender: AnyObject) {
}
}
Here are the table view related functions:
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return mealArray.orderedMeals.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
print("in")
var cell = tableView.dequeueReusableCellWithIdentifier("orderCell", forIndexPath: indexPath) as! CustomOrderTableViewCell
cell.MealName.text = mealArray.orderedMeals[indexPath.row].mealName
cell.MealDescription.text = mealArray.orderedMeals[indexPath.row].mealDescription
let price = NSString(format: "%.2f", mealArray.orderedMeals[indexPath.row].mealPrice) as String
cell.MealPrice.text = "R" + price
return cell
}
The problem is that nothing gets displayed in the table view and func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell is never called.
Any solutions? Thanks
If func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell is never called that means you have not connected your UITableViewDataSource in IB correctly. Make sure that you connect the dataSource property with your ViewController. Also check that all classes are set for your ViewController, TableView and the Cell itself as this another common mistake.

Resources