通过userdefault取消在tableview中重新加载数据

时间:2017-06-07 01:26:32

标签: swift xcode8

我是新手并且正在学习ios编程。我已经制作了一个自定义表视图单元格。我试图通过用户默认值附加数据但不存储数据,每次我返回应用程序刷新使表格视图为空白屏幕。

// data sent from here


class ViewController: UIViewController {

    @IBOutlet weak var textfield: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }


    @IBAction func submit(_ sender: AnyObject) {



        performSegue(withIdentifier: "move", sender: nil)
    }
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let destination = segue.destination as? secondviewcontroller



        destination?.list.append(textfield.text!)

        let defaults = UserDefaults.standard
        defaults.set(destination?.list, forKey: "listarray")
        UserDefaults.standard.synchronize()

    }
}


// here i want to retreive
class secondviewcontroller: UIViewController {


    @IBOutlet weak var tblView: UITableView!
    var list = ["fahad","Ali","tahir"]

   var abc = ""

override func viewDidLoad() {
    super.viewDidLoad()
    tblView.dataSource  = self;
    tblView.delegate = self;


    let storedarray = UserDefaults.standard.object(forKey: "listarray")

    print("hi\(storedarray)")
}



override func viewDidAppear(_ animated: Bool) {
     tblView.register(TableViewCell.self, forCellReuseIdentifier: "TableViewCell")
    tblView.reloadData()
}

@IBAction func backbtn(_ sender: AnyObject) {
    dismiss(animated: true, completion: nil)
}

}

导入UIKit

class secondviewcontroller:UIViewController {

@IBOutlet weak var tblView: UITableView!
var list = ["fahad","Ali","tahir"]

var abc =""

override func viewDidLoad() {
    super.viewDidLoad()
    tblView.dataSource  = self;
    tblView.delegate = self;



}



override func viewDidAppear(_ animated: Bool) {
     tblView.register(TableViewCell.self, forCellReuseIdentifier: "TableViewCell")
    let storedarray = UserDefaults.standard.object(forKey: "listarray")

    print("hi\(storedarray)")
    tblView.reloadData()
}

@IBAction func backbtn(_ sender: AnyObject) {
    dismiss(animated: true, completion: nil)
}

}

扩展名secondviewcontroller:UITableViewDataSource,UITableViewDelegate {

func numberOfSections(in tableView: UITableView) -> Int {

    return 1;
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    print (list)
    return list.count;
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    var cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell;
    cell.textlabel.text = list[indexPath.row];


    return cell;


}

}

// my custom table view
import UIKit

class   TableViewCell: UITableViewCell {

    @IBOutlet weak var textlabel: UILabel!   
    override func awakeFromNib() {
        super.awakeFromNib()
    }
}

My UI storyboard getting this error

2 个答案:

答案 0 :(得分:0)

  1. 您必须对UITableViewDelegate和UITableViewDataSource方法进行子类化,并实现所需的方法,以便在自定义视图中显示数据。 参考:http://clean-swift.com/refactoring-table-view-data-source-and-delegate-methods/
    1. 尝试将存储数组的代码添加到viewwillappear方法中。

答案 1 :(得分:0)

这些行使用新元素重新创建相同的列表,而不考虑保存的项目:

    destination?.list.append(textfield.text!)

    let defaults = UserDefaults.standard
    defaults.set(destination?.list, forKey: "listarray") 
    UserDefaults.standard.synchronize()

你应该改为:

let newEntry = textfield.text!
let defaults = UserDefaults.standard
if destination?.list.count == 3 {  //3 because you hardcoded 3 items in next controller
      destination?.list.append(newEntry) 
} else {
      let obj = defaults.object(forKey: "listarray") as? [String] ?? [String]()
      destination?.list = obj.append(newEntry)
}
defaults.set(destination?.list, forKey: "listarray")
UserDefaults.standard.synchronize()