James James - 2 years ago 66
Swift Question

Newbie - TableView Functions not getting accessed and tableView not loading

I set this up in another sample project and it works. I know there is data there as it prints out below.

import UIKit
import CoreData

class GameDataTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var GameDataTableView: UITableView!
var gameStats = [NSManagedObject]()

override func viewDidLoad() {

// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()

override func didReceiveMemoryWarning() {
// Dispose of any resources that can be recreated.

// MARK: - Table view data source

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return gameStats.count

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

print("ahere I am")

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

let game = gameStats[indexPath.row]

cell.yourTeamSaveLbl!.text = game.valueForKey("yourTeamSave")as? String
cell.yourScoreSaveLbl!.text = game.valueForKey("yourTeamScoreSave")as? String

return cell

override func viewDidAppear(animated: Bool) {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

let managedContext = appDelegate.managedObjectContext

let fetchRequest = NSFetchRequest(entityName: "TeamStats")

do {
let results = try managedContext.executeFetchRequest(fetchRequest)
gameStats = results as! [NSManagedObject]

//added line here:


for item in gameStats{

print(item.valueForKey("gameDateSave")as! String)
print(item.valueForKey("yourTeamSave")as? String)
print(item.valueForKey("yourTeamScoreSave")as? String)

print(item.valueForKey("opponentSave")as? String)
print(item.valueForKey("opponentScoreSave")as? String)
print(item.valueForKey("pplGoalSave")as? String)
print(item.valueForKey("shGoalSave")as? String)
print(item.valueForKey("shotSave")as? String)
print(item.valueForKey("passPercentSave")as? String)
print(item.valueForKey("offSideSave")as? String)
print(item.valueForKey("icingSave")as? String)
print(item.valueForKey("homeGoalSave")as? String)
print(item.valueForKey("savePercentSave")as? String)


} catch let error as NSError {
print("Coiuld not fetch \(error), \(error.userInfo)")


Answer Source

u set "gameStats" at viewDidAppear, so you have to reload tableview to get data refreshed and appeared on your tableview

so after

gameStats = results as! [NSManagedObject]

you need to add

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download