James
James

Reputation: 29

Swift: tableView.reloadData() no data in table

I am having trouble with the reloadData() in my table. I have setup the table and cell, and added tables and even made a separate class for the cell in case this was the problem. I just cannot load any data into the table..... I have read questions on here and other sites but none of the solutions are giving me solutions... I see that it is also a bug but it is working in a different project so its not my Xcode version. I have tried loading in the viewDidLoad, inside the cellForRowAt, and also using Dispatch to put it on the main thread inside and outside a function. I also tried a button in desperation but still not luck. Here is my code if you spot anything I have missed id appreciate the help.

//
//  VCRoster.swift
//  MC
//
//  Created by James McAdam on 08/07/2018.
//  Copyright © 2018 James McAdam. All rights reserved.
//

import UIKit

class VCRoster: UIViewController, UITableViewDelegate, UITableViewDataSource {

var tableViewData: [Roster] = []

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = rosterTable.dequeueReusableCell(withIdentifier: "rosterCell") as? rosterCell

    cell!.dateLbl.text = tableViewData[indexPath.row].date
    //Have also tried ? with the cell
    cell!.messageLbl.text = tableViewData[indexPath.row].message

    tableView.reloadData()
   return cell!
}


@IBOutlet weak var rosterTable: UITableView!

@IBAction func load(_ sender: UIBarButtonItem) {
      self.rosterTable.reloadData()
}


override func viewDidLoad() {
    super.viewDidLoad()

 //   dispatchMain().async {
 //       self.rosterTable.reloadData()
 //   }

     // self.rosterTable.reloadData()

    getDataFromServer(for: "Bx7faf08A9fYJ7bZCNMUX9EzxYN2") { (result) in
        // Check our result
        switch result {
        case .success(let results):
          //  print("Done")
            self.tableViewData = results.data
            //self.tablewView.reloadData()
            //self.rosterTable.reloadData()
        //    print(results)
        case .failure(let message):
        //    print("Error")
            print(message)
        }
    }

}


}

Upvotes: 1

Views: 5254

Answers (2)

Abdulaziz.Musaileekh
Abdulaziz.Musaileekh

Reputation: 325

First, you have to set a number of sections in your case should be one section contains the number of rows then you have to set the delegate & dataSource for your tableView since you are using xib or storyboard just sign do it from there or by programmatically

rosterTable.delegate = self
rosterTable.dataSource = self

make sure that your server response with the proper data and fetch out by

var tableViewData: [Roster] = [] {
 didSet {
  rosterTable.reloadData()
 }
}

DONT use reloadData while rendering the cell

Upvotes: 0

vadian
vadian

Reputation: 285069

First of all never, never, never call reloadData() in cellForRow

  • Make sure that datasource (and delegate) of the table view are connected to the view controller in Interface Builder
  • Delete tableView.reloadData() in cellForRow
  • Call reloadData() on the main thread in the completion handler in viewDidLoad

override func viewDidLoad() {
    super.viewDidLoad()

    getDataFromServer(for: "Bx7faf08A9fYJ7bZCNMUX9EzxYN2") { (result) in
        // Check our result
        switch result {
        case .success(let results):
            self.tableViewData = results.data
            DispatchQueue.main.async {
                self.rosterTable.reloadData()
            }

        case .failure(let message):
            print(message)
        }
    }

}

Upvotes: 2

Related Questions