【问题标题】:How to load array value int cell without creating multiple custom cell in UITableView iOS Swift?如何在 UITableView iOS Swift 中加载数组值 int 单元而不创建多个自定义单元?
【发布时间】:2019-12-31 19:23:08
【问题描述】:

我在 tableview 中有多个标签。为标签左侧创建了一个数组。现在我正在尝试从数组模型中为每个标签加载数据。但是它说索引超出范围,因为它的模型数组索引是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

这是我想要实现的屏幕截图:

这是我的 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": ""
  }
  ] 

任何帮助都非常感谢...

【问题讨论】:

  • 您的目标是拥有多个PrItemDetailModel 项目吗?所以你有 Material#", "Description", "Fixed Vendor#", ... "AAC" for each item?或者,你的表只是标签列表 -> 单个PrItemDetailModel的值?
  • @DonMag tableview 带有标签列表和单个值..

标签: ios arrays swift uitableview dictionary


【解决方案1】:

@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

希望对你有帮助!

【讨论】:

  • 感谢您的回答..使用返回方法时显示错误上下文类型'[[String : Any]]'不能与字典文字一起使用
  • 你能告诉我如何在我的项目中使用这个代码
  • @ParameswaranV 你能试试我编辑的答案吗?这意味着您的输出似乎有 key & values 形成了像使用单个对象数组处理的动态内容。
  • 在 viewdidload demoItems 是未解析的标识符。使用未解析的标识符“demoItems”;你是说“DemoItems”吗?
  • 好像你错过了像fileprivate var demoItems: [DemoItems]?这样的声明
【解决方案2】:

问题是您正在创建一个标签字符串数组,然后您认为您正在创建一个值数组...但您只是创建一个 ONE PrItemDetailModel 对象。

考虑一下(我已经简化了您的课程以使其更易于查看):

class PrItemDetailModel {

    var Material: String?
    var Description: String?
    var FixedVendor: String?

    init(Material: String?, Description: String?, FixedVendor: String?) {

        self.Material = Material
        self.Description = Description
        self.FixedVendor = FixedVendor

    }

}

class ViewController: UIViewController {

    // create array of Label Strings
    var PrItemTitleLblArray = ["Material#", "Description", "Fixed Vendor#"]

    // create array of PrItemDetailModel objects
    var prItemArray = [PrItemDetailModel(Material: "", Description: "Bottles", FixedVendor: "KP04")]

    override func viewDidLoad() {
        super.viewDidLoad()

        // this will print "3"
        print(PrItemTitleLblArray.count)

        // this will print "1"
        print(prItemArray.count)

    }

}

因此,当您尝试在表格中填充行时,您的第一行 - indexPath.row 等于 0 - 您的代码显示:

// get the "Label String" from the array of Label Strings
// at index 0, this will be "Material#"
cell.material.text = PrItemTitleLblArray[indexPath.row]

// get the "Value String" from the array of PrItemDetailModel objects
// at index 0, this will be AN OBJECT, not a String
cell.materialvalue = prItemArray[indexPath.row]

下一行 - indexPath.row 等于 1 - 你的代码说:

// get the "Label String" from the array of Label Strings
// at index 1, this will be "Description"
cell.material.text = PrItemTitleLblArray[indexPath.row]

// get the "Value String" from the array of PrItemDetailModel objects
// at index 1 ... ERROR, because prItemArray has only one element
cell.materialvalue = prItemArray[indexPath.row]

您可能想要重新考虑您的数据结构...您将从哪里获得这些值?通过来自服务器的 json?从数据库?最好将数据简单地存储在字典数组中。

或者,这只是另一种处理方式...修改您的类以根据索​​引返回值:

class PrItemDetailModel {

    var Material: String?
    var Description: String?
    var FixedVendor: String?

    init(Material: String?, Description: String?, FixedVendor: String?) {

        self.Material = Material
        self.Description = Description
        self.FixedVendor = FixedVendor

    }

    func getProperty(at index: Int) -> String {

        switch index {
        case 0:
            return self.Material ?? ""
        case 1:
            return self.Description ?? ""
        case 2:
            return self.FixedVendor ?? ""
        default:
            return "Error"
        }

    }

}

然后您将代码更改为:

    // create ONE PrItemDetailModel object
    var prItem = PrItemDetailModel(Material: "", Description: "Bottles", FixedVendor: "KP04")

在cellForRowAt:

// get the "Label String" from the array of Label Strings
cell.material.text = PrItemTitleLblArray[indexPath.row]

// get the "Value String" from the array of PrItemDetailModel objects
// based on the index
cell.materialvalue.text = prItem.getProperty(at: indexPath.row)

【讨论】:

  • 它工作正常。感谢您的建议。你能在传递 json.现在我使用了第二种方法..
猜你喜欢
  • 2018-06-24
  • 2015-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-15
  • 2018-06-29
  • 2015-08-26
  • 1970-01-01
相关资源
最近更新 更多