私は非常に簡単な商品注文アプリケーションを構築しています。 これはタブ付きのアプリケーションで、製品リスト、オーダーリスト、および設定です。 製品リスト(リモートcsvファイルから解析されたcoredata)から、行を選択して、行を選択して、警告表示をポップして希望の数量をテキストフィールドに追加し、alertviewの「ADD」ボタンで4つの文字列Productcode、Detail、Quantity(alertviewから)、バーコード)をOrder List View Controllerに送信します。 これはどこに問題があるのですか? OrderListで受け取った時点で4つの文字列をコンソールに出力できますが、これらの文字列をMy NSObjectモデルに渡してtableviewcellのラベルに表示して保持する方法が残っています。ここで別のtableviewcontrollerから受け取った文字列を渡す
は私のOrderProduct.swiftファイルです:
import Foundation
import UIKit
class OrderProduct: NSObject, NSCoding {
var uuid: String = NSUUID().UUIDString
var productcode:String = ""
var detail:String = ""
var quantity:String = ""
var barcode:String = ""
var inOrderList = false
func encodeWithCoder(coder: NSCoder) {
coder.encodeObject(uuid, forKey: "uuid")
coder.encodeObject(productcode, forKey: "productcode")
coder.encodeObject(detail, forKey: "detail")
coder.encodeObject(quantity, forKey: "quantity")
coder.encodeObject(barcode, forKey: "barcode")
coder.encodeBool(inOrderList, forKey: "inOrderList")
}
required init?(coder decoder: NSCoder) {
super.init()
if let archivedUuid = decoder.decodeObjectForKey("uuid") as? String {
uuid = archivedUuid
}
if let archivedProductcode = decoder.decodeObjectForKey("productcode") as? String {
productcode = archivedProductcode
}
if let archivedDetail = decoder.decodeObjectForKey("detail") as? String {
detail = archivedDetail
}
if let archivedQuantity = decoder.decodeObjectForKey("quantity") as? String {
quantity = archivedQuantity
}
if let archivedBarcode = decoder.decodeObjectForKey("barcode") as? String {
barcode = archivedBarcode
}
inOrderList = decoder.decodeBoolForKey("inOrderList")
}
init(productcode: String, detail: String, quantity: String, barcode: String) {
super.init()
self.productcode = productcode
self.detail = detail
self.quantity = quantity
self.barcode = barcode
}
}
そして、ここでは私のOrderViewController.swiftファイルです:
import Foundation
import UIKit
//MARK: - Class
class TestOrderViewController: UITableViewController{
required init?(coder decoder: NSCoder) {
//print("init OrderViewController")
super.init(coder: decoder)
loadOrderProducts()
}
deinit {
}
//MARK: - Product Vars
var passedProductcode = String() //String is recieved here!
var passedDetail = String() //String is recieved here!
var passedQuantity = String() //String is recieved here!
var passedBarcode = String() //String is recieved here!
var orderProducts = [OrderProduct]() //My Object model...
//MARK: - ViewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
title = "Order List"
orderProducts.productcode = passedProductcode //Trying to pass them but
orderProducts.detail = passedDetail //errors with "Value of type
orderProducts.quantity = passedQuantity //'[OrderProduct]' has no
orderProducts.barcode = passedBarcode //member 'productcode'?
tableView.estimatedRowHeight = 80.0
tableView.rowHeight = UITableViewAutomaticDimension
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
return orderProducts.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("OrderCell", forIndexPath: indexPath) as! OrderTableViewCell
let orderProduct = orderProducts[indexPath.row]
cell.productcodeLabel.text = orderProduct.productcode
cell.detailLabel.text = orderProduct.detail
cell.quantityLabel.text = "X \(orderProduct.quantity)"
cell.barcodeLabel.text = orderProduct.barcode
return cell
}
private func loadOrderProducts() {
if let filePath = pathForOrderProducts() where NSFileManager.defaultManager().fileExistsAtPath(filePath) {
if let archivedOrderProducts = NSKeyedUnarchiver.unarchiveObjectWithFile(filePath) as? [OrderProduct] {
orderProducts = archivedOrderProducts
}
}
}
private func pathForOrderProducts() -> String? {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
if let documents = paths.first, let documentsURL = NSURL(string: documents) {
return documentsURL.URLByAppendingPathComponent("orderProducts.plist").path
}
return nil
}
private func saveOrderProducts() {
if let filePath = pathForOrderProducts() {
NSKeyedArchiver.archiveRootObject(orderProducts, toFile: filePath)
}
}
}
誰かが私が行方不明です何に私を案内していただけますか?あなたはあなたがOrderProduct
の配列ではなく、その内部のインスタンスにプロパティ値を取得しようとしている
cell.productcodeLabel.text = orderProducts.productcode //passedProductcode
cell.detailLabel.text = orderProducts.detail //passedDetail
cell.quantityLabel.text = "X \(orderProducts.quantity/*passedQuantity*/)"
cell.barcodeLabel.text = orderProducts.barcode //passedBarcode
をしようとするときTestOrderViewController
で
せorderProduct = orderProducts [indexPath.row] cell.productcodeLabel.text = orderProduct.productcode cell.detailLabel.text = orderProduct.detail cell.quantityLabel.text = "X \(orderProduct.quantity)" cell.barcodeLabel.text = orderProduct.barcode –
ありがとう@ T.Benjamin Larsen、私の問題は実際には、変数を最初にオブジェクトに渡すことで終わりではありません。例:var passedProductcode = String()orderProducts.productcodeにどのように取得しますか? –
できません。 'orderProducts'は' OrderProduct'の_an配列です。これらのプロパティはありません。おそらく、配列にadd_this_という完全なOrderProductInstanceを作成する必要があります。私の目には、それらの「合格した」プロパティは、それほど意味をなさないように見える。 –