我在另一个示例项目中进行了设置,并且可以正常工作。我知道那里有数据,因为它在下面打印出来。
导入UIKit导入CoreData
类GameDataTableViewController:UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var GameDataTableView: UITableView!
var gameStats = [NSManagedObject]()
override func viewDidLoad() {
super.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() {
super.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:
self.tableView.reloadData()
print(gameStats.count)
print("**********************")
for item in gameStats{
print(item.valueForKey("gameDateSave")as! String)
print("------")
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)
print("**********************")
}
} catch let error as NSError {
print("Coiuld not fetch \(error), \(error.userInfo)")
}
}
您在viewDidAppear上设置“ gameStats”,因此您必须重新加载tableview才能刷新数据并显示在tableview中
所以之后
gameStats = results as! [NSManagedObject]
您需要添加
self.tableView.reloadData()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句