我的 viewDidLoad()
中有一个 UITableView,其中填充了来自 JSON 数据源的数据。我将此 JSON 数据存储在 View Controller 代码顶部的字典中:
class InventoryListViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var inventoryListTableView: UITableView!
let textCellIdentifier = "textCell";
var warehouseItems: [Inventory] = []; //array for my items
override func viewDidLoad() {
super.viewDidLoad()
inventoryListTableView.dataSource = self;
inventoryListTableView.delegate = self;
loadTableViewWithJSON(); //this loads my data into the warehouseItems dictionary created above
}
...
}
这是我的loadTableViewWithJSON()
func loadTableViewWithJSON() {
let urlString = "http://url.php";
let session = NSURLSession.sharedSession();
let url = NSURL(string: urlString)!;
session.dataTaskWithURL(url) { (data: NSData?, response:NSURLResponse?, error: NSError?) -> Void in
if let responseData = data {
do {
let json = try NSJSONSerialization.JSONObjectWithData(responseData, options: NSJSONReadingOptions.AllowFragments) as! Dictionary<String, AnyObject>;
// print(json);
if let inventoryDictionary = json["inventory"] as? [Dictionary<String, AnyObject>] {
// print(inventoryDictionary);
for anItem in inventoryDictionary {
// print(anItem["quantityOnHand"] as? Int); //works
if let id = anItem["id"] as? Int, let item = anItem["item"] as? String, let description = anItem["description"] as? String, let quantityOnHand = anItem["quantityOnHand"] as? Int, let supplierId = anItem["supplier_id"] as? Int, let supplierName = anItem["supplierName"] as? String {
let item = Inventory(id: id, item: item, description: description, quantityOnHand: quantityOnHand, supplierId: supplierId, supplierName: supplierName);
// print(item);
self.warehouseItems.append(item);
}
}
// print(self.warehouseItems[0].description); //works
dispatch_async(dispatch_get_main_queue(), {
self.inventoryListTableView.reloadData();
})
}
} catch {
print("Could not serialize");
}
}
}.resume()
}
点击其中一个项目会进入另一个 View 以编辑被点击的项目。当更改字段并编辑项目时,会出现一个大的绿色复选标记,它调用 rest API 并更新我的数据库(我的 UITableView 从中获取)中的数据。现在,当我单击 NavigationController 中的“后退”按钮时,它会返回到带有 UITableView 的上一个屏幕,但值不会更新。我试过在我的 viewWillAppear()
中运行 tableView.reloadData()
但没有任何效果。我也试过调用我的函数来从 JSON 中获取数据,但当然只是将更新的数据添加到我的 tableView 的末尾,我有一个旧的数据副本,下面有一个新的。
我的问题是,我是否应该只清除我的 UITableView 并再次运行我的函数来获取数据?或者我应该只用新数据更新单行吗?我认为更新单行会更好,因为我的 restful API 开销更少。或者检测我何时从编辑 View Controller 返回并重新加载所有数据会更好吗?
我已经试过了,但它不起作用:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated);
// loadTableViewWithJSON(); // this duplicates the data in the tableview
inventoryListTableView.reloadData();
}
还有我的cellForRowAtIndexPath
:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as UITableViewCell;
let row = indexPath.row;
cell.textLabel!.text = "\(warehouseItems[row].item) --> \(warehouseItems[row].quantityOnHand) on hand"
cell.detailTextLabel!.text = warehouseItems[row].description;
return cell;
}
最佳答案
当您调用 loadTableViewWithJSON()
时,您正在调用 self.warehouseItems.append(item)
,它只是附加到现有数组。这就是您在 tableView 中看到重复数据的原因。
将 warehouseItems = []
添加到 session.dataTaskWithURL(url)
响应 block 的顶部。这将清除阵列,以便您可以使用更新后的数据从头开始重建它。
func loadTableViewWithJSON() {
let urlString = "http://url.php";
let session = NSURLSession.sharedSession();
let url = NSURL(string: urlString)!;
session.dataTaskWithURL(url) { (data: NSData?, response:NSURLResponse?, error: NSError?) -> Void in
if let responseData = data {
warehouseItems = []
完成这些更改后,以下内容应该对您有用。
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
loadTableViewWithJSON()
inventoryListTableView.reloadData()
}
最后请注意,Swift 中不需要分号,我们有空!
关于ios - 从编辑表单返回时更新 UITableView,刷新整个表数据源或被编辑的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34642233/