我試圖讓程序檢查導航標題,然後相應地填充單元格。硬編碼數組很容易,但使用核心數據對象會讓事情變得艱難。根據頁面標題動態填充單元格
初始化器 - 3個產品爲每個公司和空數組檢查網頁的標題後,將它們設置爲:
let defaultProductsApple = ["iPhone", "iPad", "Macbook"]
let defaultProductsGoogle = ["Search", "Firebase", "Magic Leap"]
let defaultProductsFacebook = ["Facebook", "Instagram", "WhatsApp"]
let defaultProductsTesla = ["Model S", "Model X", "PowerWall"]
let defaultProductsTwitter = ["Twitter", "Periscope", "Vine"]
var defaultProducts = [String]()
viewDidLoad中 - 檢查頁面標題和填充空數組,然後設置所需核心數據刪除持久性時:
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(Cell.self, forCellReuseIdentifier: cellId)
// Navbar stuff
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(handleBackButton))
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(handleAdd))
let entity = NSEntityDescription.entity(forEntityName: "Product", in: managedContext)!
switch self.navigationItem.title! {
case "Apple":
defaultProducts = defaultProductsApple
case "Google":
defaultProducts = defaultProductsGoogle
case "Twitter":
defaultProducts = defaultProductsTwitter
case "Tesla":
defaultProducts = defaultProductsTesla
case "Facebook":
defaultProducts = defaultProductsFacebook
default:
defaultProducts = defaultProductsApple
}
for productName in defaultProducts {
let product = NSManagedObject(entity: entity,insertInto: managedContext)
product.setValue(productName, forKey: "name")
products.append(product)
}
do {
try managedContext.save()
tableView.reloadData()
} catch let error as NSError {
print("Could not save. \(error), \(error.userInfo)")
}
}
最後的標籤設置在cellForRow:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! Cell
let product = products[indexPath.row]
cell.textLabel?.text = product.value(forKey: "name") as? String
return cell
}
單元格應該在第一次嘗試時填充 - 即如果在前一個屏幕中點擊「Apple」,單元格會顯示Apple的3種產品。但是如果我回過頭來點擊「Google」,那麼這些單元現在會展示Apple的3款產品以及Google的3款產品,等等。
我該怎麼做才能使cell.textLabel?.text = product.value(forKey: "name") as? String
只加載該公司的產品?
編輯:viewWillAppear
和save
方法:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//1
guard let appDelegate =
UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext =
appDelegate.persistentContainer.viewContext
//2
let fetchRequest =
NSFetchRequest<NSManagedObject>(entityName: "Product")
//3
do {
products = try managedContext.fetch(fetchRequest)
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}
}
func save(name: String) {
guard let appDelegate =
UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext =
appDelegate.persistentContainer.viewContext
let entity =
NSEntityDescription.entity(forEntityName: "Product",
in: managedContext)!
let product = NSManagedObject(entity: entity,
insertInto: managedContext)
product.setValue(name, forKey: "name")
do {
try managedContext.save()
products.append(product)
} catch let error as NSError {
print("Could not save. \(error), \(error.userInfo)")
}
}
最後編輯 - 在一個設定的所有默認值去
// Default 5 companies at launch - user can delete/add/edit
func setDefaults() {
let userDefaults = UserDefaults.standard
let defaultValues = ["firstRun" : true]
userDefaults.register(defaults: defaultValues)
if userDefaults.bool(forKey: "firstRun") {
let defaultProducts = ["Apple" : ["iPhone", "iPad", "Macbook"],
"Google" : ["Search", "Firebase", "Magic Leap"],
"Facebook" : ["Facebook", "Instagram", "WhatsApp"],
"Tesla" : ["Model S", "Model X", "PowerWall"],
"Twitter" : ["Twitter", "Periscope", "Vine"]]
let companyEntity = NSEntityDescription.entity(forEntityName: "Company", in: managedContext)!
let productEntity = NSEntityDescription.entity(forEntityName: "Product", in: managedContext)!
// Setting the default company data (name, logo, and stockPrice)
let url = URL(string: "https://query.yahooapis.com/v1/public/yql?q=select%20symbol%2C%20Ask%2C%20YearHigh%2C%20YearLow%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22AAPL%22%2C%22GOOG%22%2C%22TWTR%22%2C%22TSLA%22%2C%20%22FB%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys")!
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil {
print(error!)
} else if let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 {
let json = JSON(data: data!)
if let quotes = json["query"]["results"]["quote"].array {
for quote in quotes {
let symbol = quote["symbol"].stringValue
let name = NSLocalizedString(symbol, comment:"")
let ask = quote["Ask"].stringValue
let company = NSManagedObject(entity: companyEntity,insertInto: managedContext)
company.setValue(name, forKey: "name")
company.setValue(name, forKey: "logo")
company.setValue(ask, forKey: "stockPrice")
companies.append(company)
print(json)
}
// Setting the default products - should I be doing this here too?
let companyProducts = defaultProducts[companyName]
// iterate through all those products names...
for productName in companyProducts {
// create the corresponding Product:
let product = NSManagedObject(entity: productEntity,insertInto: managedContext)
// set its name attribute...
product.setValue(productName, forKey: "name")
// then set the relationship to the relevant Company...
product.setValue(company, forKey: "company")
}
DispatchQueue.main.async {
do {
try managedContext.save()
vc.tableView.reloadData()
userDefaults.set(false, forKey: "firstRun")
} catch let error as NSError {
print("Could not save. \(error), \(error.userInfo)")
}
}
}
} else {
print("The data couldn't be loaded")
}
}
task.resume()
}
}
你有,也許通過執行讀取請求,例如更新'products'陣列,任何其他代碼。在'viewWillAppear'還是其他生命週期方法之一? – pbasdf
@pbasdf是的,我編輯我的問題,包括'viewWillAppear'以及'save'方法。任何幫助將大力讚賞! – d0xi45
謝謝 - 這解釋了你正在觀察。獲取請求(在viewWillAppear中)獲取所有內容;你需要一些方法來區分蘋果的產品和谷歌(和其他人)的產品。您可以通過向產品實體添加「公司」屬性或向「公司」實體添加關係來實現此目的。您還需要修改代碼以僅加載一次這些默認值。稍後我會添加完整的答案... – pbasdf