NsUserDefualts无法正常运行 [英] NsUserDefualts not functioning right

查看:138
本文介绍了NsUserDefualts无法正常运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我在使用tableView时遇到了一些错误,我注意到我的tableView并不总是得到正确的更新,因此我尝试对其进行调试,而我注意到的是,并非总是可以调用tableView类来更新表.我究竟做错了什么 ?当我向表中添加新条目时,计数为4 + 1,我转到历史记录"选项卡,但未发生任何事情,并且由于计数仍为4而显示,但是如果我再切换选项卡1,它将显示为5,tableView将为已更新..因此由于某种原因更新存在延迟,我可以添加刷新按钮,但我不想这样做..

Basically I have some sort of bug with tableView, i noticed that my tableView is not always getting updated correctly, and i tried debugging it and what i noticed is that tableView class is not always getting called to update the table. What am i doing wrong ? When i add new entry to my table, count 4 + 1, I go to history tab, and nothing happens and it shows as the count is still 4 but if I switch tabs 1 more time it will show count as 5 and tableView will be updated.. so there is a delay in update for some reason, i could add a refresh button but i don't want to do that..

//
//  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)
    }


}

推荐答案

以下几点:

  1. 将数据存储为用户默认设置后,请调用一次synchronize,以便将更改保存到永久存储中.

  1. After storing your data in user defaults, call synchronize once so changes are saved to permanent storage.

不要对表视图(tableView(_: cellForRowAtIndexPath:))的每一行使用默认值.相反,请一次读取它们并将数据存储在array/dictionary属性中,然后使用该属性按需配置每个单元.

Don't red the defaults for each row of your table view (tableView(_: cellForRowAtIndexPath:)). Instead, read them once and store the data in an array/dictionary property, and use that to configure each cell on demand.

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

Regarding advice #2: I don't think reading the user's defaults is too slow, but I'm not sure how big your archived data is and that method gets called for each row that the table view needs to display on screen. Be efficient!

这篇关于NsUserDefualts无法正常运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆