更新:我的修复
这解决了我的问题,它可能对您有帮助!
override func viewDidAppear(animated: Bool) {
authors = loadPlist()
tableView.reloadData()
}
现在数据加载完美:)
我的代码遇到问题。我有一个 plist,正在修改并向其添加条目。
一切工作正常,但我需要重新启动 iOS 模拟器才能看到新添加的 plist 条目。
快速总结一下,plist 文件中的所有内容都会更新,但需要重新构建应用程序。
我尝试过 tableView.reloadData() 但我的理解是一旦 viewDidLoad() 只运行一次。
现在我尝试通过在前后创建额外的 Segue 连接来绕过这个问题,但我发现这适得其反,而且很麻烦。
非常感谢!
附注
我感觉我没有正确保存数据?
创建副本时如何从 plist 检索信息:
private func loadPlist() -> NSArray {
var data = NSArray()
// attempt to open "authors.plist" from the application's Documents/ directory
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
as NSArray
let documentsDirectory = paths.objectAtIndex(0) as! NSString
let path = documentsDirectory.stringByAppendingPathComponent("authors.plist")
let fileManager = NSFileManager.defaultManager()
// if the file is not available (first access), then open it from the app's
// mainBundle Resources/ folder:
if(!fileManager.fileExistsAtPath(path)) {
let plistPath = NSBundle.mainBundle().pathForResource("authors", ofType: "plist")
data = NSArray(contentsOfFile: plistPath!)!
do {
try fileManager.copyItemAtPath(plistPath!, toPath: path)
} catch let error as NSError {
// failure
print("Error copying plist file: \(error.localizedDescription)")
}
print("First launch... Default plist file copied...")
data.writeToFile(path, atomically: true)
}
else {
data = (NSArray(contentsOfFile: path))!
}
return data
}
保存数据:
func saveData(){
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
as NSArray
let documentsDirectory = paths.objectAtIndex(0) as! NSString
let path = documentsDirectory.stringByAppendingPathComponent("authors.plist")
if let plistArray = NSMutableArray(contentsOfFile: path) {
//...
//...
plistArray.writeToFile(path, atomically: false)
}
}
保存按钮:
@IBAction func saveDataButton(sender: UIBarButtonItem) {
saveData()
navigationController?.popViewControllerAnimated(true)
}
我如何填充我的桌面 View :
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)
if let author = authors[indexPath.row] as? [String: AnyObject], let name = author["Author"] as? String {
// Configure Cell
cell.textLabel?.text = name
}
return cell;
我的 viewDidLoad() 现在:
override func viewDidLoad() {
super.viewDidLoad()
title = "Authors"
authors = loadPlist()
tableView.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: cellIdentifier)
tableView.delegate = self;
tableView.dataSource = self;
tableView.reloadData()
}
最佳答案
你看起来很困惑。是的,viewDidLoad 在 View Controller 的生命周期中只被调用一次。
您应该在 viewDidLoad 中调用一次 loadData 方法。
在返回数组之前,您应该更改 loadData 以制作数组的可变副本。您应该将可变数组保留在 View Controller 的属性中。这将成为您的 TableView 的模型(数据存储)。
当用户对 TableView 中的条目进行更改时,将这些更改应用到可变数组。让您的 TableView 数据源方法从该可变数组中获取数据。
您的 storeData 方法错误。它首先从磁盘读取 plist(清除用户所做的任何更改),然后将其写回。最终的结果就是什么也没做。在函数开头读取读取内容,并将其中发生更改的可变数组保存回 plist 文件。
关于ios - Swift - 使用 plist 的 tableview 在添加新数据后不会重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36058123/