PvDev
PvDev

Reputation: 829

How to append array of value into tableView in iOS Swift?

TableView consists with list of dynamic label and navigation bar consists of two button called up and down.

here i'm getting data from json and append to model. But i can not able to list in tableview. Array value should be loaded to list of label.

Here is my json appended to model:

   [PO_PR.poItemsModel(Material: Optional("RMECC_MOB1"), Qty: Optional("2.000"), UoM: Optional("KG"), totalValue: Optional("1000.000"), Requistor: Optional(""), StorageLocation: Optional("3001"), MatGrp: Optional("00107"), valuationPrice: Optional("1000.000"), ItemCategory: Optional("0"), lineNo: Optional("00010"), UnitofPrice: Optional("1.000")),

PO_PR.poItemsModel(Material: Optional("RMECC_MOB1"), Qty: Optional("2.000"), UoM: Optional("KG"), totalValue: Optional("1000.000"), Requistor: Optional(""), StorageLocation: Optional("3001"), MatGrp: Optional("00107"), valuationPrice: Optional("1000.000"), ItemCategory: Optional("0"), lineNo: Optional("00020"), UnitofPrice: Optional("1.000"))] 

Initially array of first index should be loaded to list of label and then when pressing down button next of array value should be loaded to list of label.

Labels in tableview will be in each cell not in a single cell.

Here is my model:

struct poItemsModel {

var Material       : String?
var Qty            : String?
var UoM            : String?
var totalValue     : String?
var Requistor      : String?
var StorageLocation: String?
var MatGrp         : String?
var valuationPrice : String?
var ItemCategory   : String?
var lineNo         : String?
var UnitofPrice    : String?

init(json: JSON) {

Material = json["Material"].stringValue
Qty = json["Quantity"].stringValue
UoM = json["OrderUnit"].stringValue
totalValue = json["NetPrice"].stringValue
Requistor = json["Requistor"].stringValue
StorageLocation = json["StorageLocation"].stringValue
MatGrp = json["MatGroup"].stringValue
valuationPrice = json["NetPrice"].stringValue
ItemCategory = json["ItemCat"].stringValue
lineNo = json["Item"].stringValue
UnitofPrice = json["UnitofPrice"].stringValue

}
 }

Here i'm appeding json data to model:

    func GetPoItemCount() {

      if orderNo != nil {
    // Call API
        print("orderni::\(orderNo!)")
        PoVendorListApiManager.sharedInstance.getPoListWithModel(orderString: orderNo!){ (json:JSON) in
        // return json from API
            if let results = json["d"]["results"].array {
                print("catefory::\(results)")


                for category in results {

                     self.PoItemsArray.append(poItemsModel(json: category))

                    for (key, value) in category {
                        print("keyvalue\(key)\(value)")

                        self.poItemJsonArray.append(value)
                        self.poItemKeyArray.append(key)
                        let currency = newPrItemDetailModel(key: key, value: value)
                        self.newItemDetailsArray.append(currency)

                      }

                }

                let elm = self.PoItemsArray
                let mirror = Mirror(reflecting: elm)
                for child in mirror.children  {
                    print("key: \(String(describing: child.label)), value: \(child.value)")
                }

                print("self.PoItemsArray array:::\(self.PoItemsArray)")
                print("new model array:::\(self.newItemDetailsArray)")

                DispatchQueue.main.async { 
                    self.tableView.reloadData()

                }
            }
        }
     }
   }

  }//extension

Here is my tableview code:

      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    //        PoItems = loadPoItems()
    print("count::itemArray::\(self.PoItemsArray.count)")
    return PoItemsArray.count

   }

   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

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

    let itemDetails = PoItemsArray[indexPath.row]
   //        for arrayData in

  //        PoItems = loadPoItems()

 //        prItem.getProperty(at: indexPath.row)

 //        cell.materials.text = PoItemsArray[0].key!
 //        cell.materialsValue.text = itemDetails[indexPath.row]



    return cell

  }

Here is my screenshot where i'm trying to achieve: enter image description here

How to load to array of first index into list of dynamic labels and when pressing down button how to load next array index of values into list of dynamic labels.

Any help much appreciated pls....

Upvotes: 1

Views: 771

Answers (1)

Fattie
Fattie

Reputation: 12625

This can not be done (realistically) with a table view. Table views are for long lists (a variable number) of the same item which can not change. What you have here is a fixed number (9) of totally different items, which do change.

Answer 1:

Do not use a table view for this. It's not what a table view is for.

Simply use a StackView for your layout - it's dead easy, and still scrolls etc.

Done. It will take 2 minutes.


Answer 2:

If as an exercise you want to use a table view, which would be wrong:

To do "synced / dynamic" tables is extremely difficult.

  1. You must have a custom cell class

  2. The button (on the main screen) must signal in some way to all of the cells to make the change in question.

This is a broad field of enquiry, you'd have to look in to the various ways to achieve this.

Upvotes: 1

Related Questions