gpt4 book ai didi

ios - 如何在不在 UITableView iOS Swift 中创建多个自定义单元格的情况下加载数组值 int 单元格?

转载 作者:可可西里 更新时间:2023-11-01 01:06:56 26 4
gpt4 key购买 nike

我在表格 View 中有多个标签。为标签左侧创建了一个数组。现在我正在尝试从每个标签的数组模型加载数据。但它说索引超出范围,因为它的模型数组索引为 0。

这是我的模型:

 class PrItemDetailModel {

var Material: String?
var Description: String?
var FixedVendor: String?
var VendorName: String?
var PurchOrg: String?
var PurchGroup: String?
var qtyUoM: String?
var valuationPrice: String?
var TotalValue: String?
var DeliveryDate: String?
var MatGrp: String?
var Plant: String?
var StorageLocation: String?
var ReqTrack: String?
var AAC: String?
var ItemCategory: String?
var GLAccount: String?
var CostCenter: String?
var Order: String?
var WBS: String?
var ProfitCenter: String?


init(Material: String?, Description: String?, FixedVendor: String?, VendorName:String?, PurchOrg: String?, PurchGroup: String?, qtyUoM: String?, valuationPrice: String?, TotalValue: String?, DeliveryDate: String?, MatGrp: String?, Plant: String?, StorageLocation: String?, ReqTrack: String?, AAC: String?, ItemCategory: String?, GLAccount: String?, CostCenter: String?, Order: String?, WBS: String?, ProfitCenter: String?){

self.Material = Material
self.Description = Description
self.FixedVendor = FixedVendor
self.VendorName = VendorName
self.PurchOrg = PurchOrg
self.PurchGroup = PurchGroup
self.qtyUoM = qtyUoM
self.valuationPrice = valuationPrice
self.TotalValue = TotalValue
self.DeliveryDate = DeliveryDate
self.MatGrp = MatGrp
self.Plant = Plant
self.StorageLocation = StorageLocation
self.ReqTrack = ReqTrack
self.AAC = AAC
self.ItemCategory = ItemCategory
self.GLAccount = GLAccount
self.CostCenter = CostCenter
self.Order = Order
self.WBS = WBS
self.ProfitCenter = ProfitCenter

}

}

这是为标签创建的数组:

   var PrItemTitleLblArray = ["Material#", "Description", "Fixed Vendor#","Vendor Name", "Purch Org", "Purch Group", "Qty | UoM", "valuation Price", "Total Value", "Delivery Date", "Mat Grp", "Plant", "Storage Location", "Req Track#", "AAC", "Item Category", "G/L Account", "Cost Center", "Order", "WBS", "Profit Center"]

这是来自模型的数组:

  var prItemArray = [PrItemDetailModel(Material: "", Description: "Bottles", FixedVendor: "KP04", VendorName: "KP Suppliers", PurchOrg: "1000", PurchGroup: "002", qtyUoM: "40.000 EA", valuationPrice: "EUR 40.00", TotalValue: "EUR 1,600.00", DeliveryDate: "09/12/2014", MatGrp: "00807", Plant: "1100", StorageLocation: "", ReqTrack: "", AAC: "Cost Center", ItemCategory: "0", GLAccount: "000040000", CostCenter: "0000010000", Order: "", WBS: "0000000", ProfitCenter: "0000001402")]

这是我在 tableview 中使用的代码:

       let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! PrItemCellTableViewCell

cell.material.text = PrItemTitleLblArray[indexPath.row]
cell.materialvalue = prItemArray[indexPath.row]

return cell

这是我想要实现的屏幕截图:enter image description here

这是我的 json 响应:

 "results": [
{

"PoDocNo": "4500022400",
"Item": "00010",
"Material": "RMECC_MOB1",
"StorageLocation": "3001",
"MatGroup": "00107",
"Quantity": "2.000",
"OrderUnit": "KG",
"NetPrice": "1000.000",
"UnitofPrice": "1.000",
"ItemCat": "0",
"Requistor": ""
},
{

"PoDocNo": "4500022400",
"Item": "00020",
"Material": "RMECC_MOB1",
"StorageLocation": "3001",
"MatGroup": "00107",
"Quantity": "2.000",
"OrderUnit": "KG",
"NetPrice": "1000.000",
"UnitofPrice": "1.000",
"ItemCat": "0",
"Requistor": ""
}
]

非常感谢任何帮助...

最佳答案

@Parameswaran V

在下面检查我的简单更新答案 你能建议我在传递 json 时最好的方法吗

您可以创建结构来形成您需要的记录,如下所示,

    struct DemoItems {

var key: String?
var value: String?
}

fileprivate var demoItems: [DemoItems]?

fileprivate func loadDemoItems() -> [DemoItems] {

var tempItems = [DemoItems]()

let item1 = DemoItems.init(key: "Material#", value: "")
let item2 = DemoItems.init(key: "Description", value: "Bottles")
let item3 = DemoItems.init(key: "Fixed Vendor#", value: "KP04")
let item4 = DemoItems.init(key: "Vendor Name", value: "KP Suppliers")
let item5 = DemoItems.init(key: "Purch Org", value: "1000")

tempItems.append(item1)
tempItems.append(item2)
tempItems.append(item3)
tempItems.append(item4)
tempItems.append(item5)

return tempItems
}

在viewDidLoad之类的,

    demoItems = loadDemoItems()

if let tempDemoItems = demoItems {

print(tempDemoItems)
print(tempDemoItems[0].key)
print(tempDemoItems[0].value)
}

输出:

[permisdesauver_dev.DemoItems(key: Optional("Material#"), value: Optional("")), permisdesauver_dev.DemoItems(key: Optional("Description"), value: Optional("Bottles")), permisdesauver_dev.DemoItems(key: Optional("Fixed Vendor#"), value: Optional("KP04")), permisdesauver_dev.DemoItems(key: Optional("Vendor Name"), value: Optional("KP Suppliers")), permisdesauver_dev.DemoItems(key: Optional("Purch Org"), value: Optional("1000"))]
Optional("Material#")
Optional("")

所以你的 cell.material.text = tempDemoItems[indexPath.row].key & cell.materialvalue.text = tempDemoItems[indexPath.row].value

希望对您有所帮助!

关于ios - 如何在不在 UITableView iOS Swift 中创建多个自定义单元格的情况下加载数组值 int 单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57677604/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com