我正在构建一个非常简单的产品订购应用程序。 这是一个选项卡式应用程序,包括产品列表、订单列表和设置。 从产品列表(从远程 csv 文件解析的核心数据)中,我可以搜索然后选择一行,然后弹出一个警报 View 以将所需数量添加到文本字段,然后在警报 View 中的“添加”按钮上我可以使用四个字符串(产品代码、详细信息、数量(来自警报 View )和条形码)到订单 ListView Controller 。 这就是我遇到麻烦的地方! 我可以在 OrderList 接收时在控制台中打印四个字符串,但我一直在寻找一种将这些字符串传递到 My NSObject 模型中的方法,以便在 tableviewcell 的标签中显示和保留它们。
这是我的 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)
}
}
}
有人可以指导我找到我所缺少的东西吗?
最佳答案
您的orderProducts
属性(property) TestOrderViewController
是一个数组 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
您正在尝试检索 OrderProduct
数组上的属性值而不是其中的实例。它可能应该是:
cell.productcodeLabel.text = orderProducts[indexPath.row].productcode //passedProductcode
cell.detailLabel.text = orderProducts[indexPath.row].detail //passedDetail
cell.quantityLabel.text = "X \(orderProducts.quantity/*passedQuantity*/)"
cell.barcodeLabel.text = orderProducts[indexPath.row].barcode //passedBarcode
但是您需要确保您的数组包含正确的模型(显然)。
<小时/>好的,正如所 promise 的,我会尝试制作一个更完整的示例。这可能不会 100% 符合您的要求(无论如何我都完全清楚这一点),但希望它能让您上路...
在实际传递值的 viewController 中,我建议您传递模型对象数组。这可能是这样的:
struct ProductModel {
var productcode: String
var detail: String
var quantity: String
var barcode: String
}
var models = [ProductModel]()
因此,您将创建 ProductModel 的实例并将它们附加到数组中。当您准备好移动到下一个 viewController TestOrderViewController
时从它的外观来看,您将向它传递整个数组(因为它将有一个匹配的属性)。
这将在您推送 viewController 之前完成,或者根据您处理 viewController 的方式在prepareForSegue 中完成。
然后在 TestOrderViewController
你会得到类似下面的东西:
// UITableViewDataSource
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return models.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("OrderCell", forIndexPath: indexPath) as! OrderTableViewCell
let model = models[indexPath.row]
cell.productcodeLabel.text = model.productcode
cell.detailLabel.text = model.detail
cell.quantityLabel.text = "X \(model.quantity)"
cell.barcodeLabel.text = model.barcode
return cell
}
关于swift - 传入从另一个 tableviewcontroller 接收到的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37872442/