新手 - TableView函数无法访问和tableView无法加载

时间:2016-04-17 12:09:15

标签: swift uitableview

我在另一个示例项目中设置它并且它可以工作。我知道那里有数据,因为它打印在下面。

导入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)")
    }


}

2 个答案:

答案 0 :(得分:1)

你在viewDidAppear设置“gameStats”,所以你必须重新加载tableview才能刷新数据并出现在你的tableview上

所以

之后
self.tableView.reloadData()

你需要添加

  @Override
    public void onSensorChanged(SensorEvent event) {


       float[] g = new float[3];
        g = event.values.clone();

      double norm_Of_g = Math.sqrt(g[0] * g[0] + g[1] * g[1] + g[2] * g[2]);

        Normalize the accelerometer vector
        g[0] = (float) (g[0] / norm_Of_g);
        g[1] = (float) (g[1] / norm_Of_g);
       g[2] = (float) (g[2] / norm_Of_g);
        int inclination = (int) Math.round(Math.toDegrees(Math.acos(g[2])));
        int rotation = (int) Math.round(Math.toDegrees(Math.atan2(g[0],              g[1])));
       if (rotation < 25 || rotation > 100)

答案 1 :(得分:0)

写下新答案,因为我还没有发表评论。看来,当你设置&#34; gameStats&#34;您正尝试使用&#34; self.tableView.reloadData()&#34;重新加载tableView。但是在代码的开头你引用了tableView作为&#34; GameDataTableView&#34;。尝试更改&#34; self.tableView.reloadData()&#34;使用&#34; self.GameDataTableView.reloadData()&#34;看看它是否有帮助。