0

基本上我有一些关于 tableView 的错误,我注意到我的 tableView 并不总是正确更新,我尝试调试它,我注意到 tableView 类并不总是被调用来更新表。我究竟做错了什么 ?当我向我的表添加新条目时,计数 4 + 1,我转到历史选项卡,没有任何反应,它显示为计数仍为 4,但如果我再切换选项卡 1 次,它将显示计数为 5,tableView 将是更新了..所以由于某种原因更新有延迟,我可以添加一个刷新按钮,但我不想这样做..

//
//  SecondViewController.swift
//
//  Created by Artiom Sobol on 1/3/16.
//  Copyright © 2016 Artiom Sobol. All rights reserved.
//

import UIKit

class History: UIViewController, UITableViewDataSource, UITableViewDelegate
{
    // test variable
    var test: MyHistory!
    // array to store unarchived history
    var newHistory = [MyHistory]()

    //outlet for tableview

    @IBOutlet var tableView: UITableView!


    override func viewDidLoad()
    {
        //change the background
        self.view.backgroundColor = UIColor(patternImage: UIImage(named: "newBackground.jpg")!)
        super.viewDidLoad()

        //self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "historyCell")
        //unarchive any new data
        let defaults = NSUserDefaults.standardUserDefaults()

        if let savedPeople = defaults.objectForKey("MyHistory") as? NSData {
            newHistory = NSKeyedUnarchiver.unarchiveObjectWithData(savedPeople) as! [MyHistory]
        }
        tableView.delegate = self
        tableView.dataSource = self
        tableView.reloadData()
    }



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

    func numberOfSectionsInTableView(tableView: UITableView) -> Int
    {
        return 1
    }

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

        let cell = tableView.dequeueReusableCellWithIdentifier("historyCell", forIndexPath: indexPath) as! historyCell
        let person = newHistory[indexPath.item]
        let defaults2 = NSUserDefaults.standardUserDefaults()

        print("This is count", newHistory.count)

        if let savedPeople = defaults2.objectForKey("MyHistory") as? NSData {
            newHistory = NSKeyedUnarchiver.unarchiveObjectWithData(savedPeople) as! [MyHistory]
        }



       // cell.durationLabel.text = String(person.durationNumber)
        let (hour,minutes,seconds) = secondsToHoursMinutesSeconds(person.durationNumber)

        if(seconds < 10 && minutes < 10)
        {
            cell.durationLabel.text = "0\(hour):0\(minutes):0\(seconds)"
        }
        else if(seconds > 9 && minutes < 10)
        {
            cell.durationLabel.text = "0\(hour):0\(minutes):\(seconds)"
        }
        else if(seconds > 9 && minutes > 9)
        {
            cell.durationLabel.text = "0\(hour):\(minutes):\(seconds)"
        }
        else if(seconds < 10 && minutes > 9)
        {
            cell.durationLabel.text = "0\(hour):\(minutes):0\(seconds)"
        }


        cell.kicksLabel.text = String(person.kicksNumber)

        return cell
    }


    func secondsToHoursMinutesSeconds (seconds : Int) -> (Int, Int, Int)
    {
        return (seconds / 3600, (seconds % 3600) / 60, (seconds % 3600) % 60)
    }


}
4

2 回答 2

0

reloadData 将在您的数组更改时调用,如果您不更改数组,或者当您调用reloadData但没有数组更新时,没有效果。

所以基本上,每次你更新数组时,调用reloadData主队列(如果你的数组更新在另一个队列中,这是必须的)

所以对代码:

    if let savedPeople = defaults.objectForKey("MyHistory") as? NSData {
        newHistory = NSKeyedUnarchiver.unarchiveObjectWithData(savedPeople) as! [MyHistory]
    }
    tableView.delegate = self
    tableView.dataSource = self
    tableView.reloadData()

当视图控制器加载时,你只调用reloadData()一次viewDidLoad,它只调用一次。

您可以执行以下操作:

self.newHistory = getUpdated()

dispatch_async(dispatch_get_main_queue(), {
    self.tableView.reloadData()
})
于 2016-01-25T05:41:35.097 回答
0

有几件事:

  1. 将数据存储在用户默认值中后,调用synchronize一次,以便将更改保存到永久存储中。

  2. 不要将表格视图 ( tableView(_: cellForRowAtIndexPath:)) 的每一行的默认值变红。相反,读取它们一次并将数据存储在数组/字典属性中,并使用它来按需配置每个单元格。

关于建议 2:我不认为读取用户的默认值太慢,但我不确定您的存档数据有多大,并且该方法会为表格视图需要在屏幕上显示的每一行调用。要有效率!

于 2016-01-25T05:44:28.087 回答