1

I have displayed first json data in tableview but when trying to display array data of inner array data it's showing blank data on table view , I have tired many ways sometimes it showing me Index out of range don't know where i'am getting wrong or forget to write code , I'am able to display that Fees class data in table view but not able to display description data ,Or do i need to change my Ui desgin

here i have model class

class Fees {
    var Billno = String()
    var DateOfReciept = String()
    var amount =  String()
    var status = String()
    var recivedDate = String()
    var AmountPaid = String()
    var descriptions = [Description]()


    init(feesJson:JSON) {
        self.Billno = feesJson["RecieptNo"].stringValue
        self.DateOfReciept = feesJson["DateOfReciept"].stringValue
        self.amount = feesJson["dueAmount"].stringValue
        self.status = feesJson["Status"].stringValue
        self.recivedDate = feesJson["recivedDate"].stringValue
        self.AmountPaid = feesJson["AmountPaid"].stringValue

        if let description = feesJson["Description"] as? JSON, let desArray = description.array{
            for desc in desArray{
                let desfees = Description(feedesJson: desc)
                self.descriptions.append(desfees)
            }
        }
    }

}

class Description{
var amountdes =  String()
    var des = String()

    init(feedesJson:JSON){
        self.amountdes = feedesJson["Amount"].stringValue
        self.des = feedesJson["des"].stringValue
    }

}

code for getting JSON data

@IBOutlet weak var tableview1: UITableView!
    var fees : [Fees] = []
    var descriptionFe : [Description] = []
func getFees() {
        let defaults = UserDefaults.standard
        let student_id =  defaults.string(forKey: "masteridKey")


        let std_id_String = student_id?.replacingOccurrences(of: "[^0-9 ]", with: "", options: NSString.CompareOptions.regularExpression, range:nil)
        print("numbericpending",std_id_String!)

        let url = NSURL(string: "http://192.168.100.5:84/api/financeApi/getAllFees" + "?ID=" + std_id_String! + "&fromDate=" + "&toDate=")
        var request = URLRequest(url: url! as URL)
        request.httpMethod = "GET"
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        Alamofire.request(request).responseJSON(){ response in
            switch response.result{
            case.success(let data):
                print("success",data)

                let myresponse = JSON(data)

                print("tabledata",myresponse)

               // dictdata = myresponse.arrayObject

                for fee in myresponse.array!
                {
                    let feesObj = Fees(feesJson: fee)
                    self.fees.append(feesObj) // here I'am getting array data but not descripition datas and while calling from here to tableview its giving me blank


                }


                for fee in myresponse.array!
                {
                //self.descriptionFe = feesObj.descriptions

                let feesdesc =  Description(feedesJson: fee)

                self.descriptionFe.append(feesdesc)

                }
                self.tableview1.reloadData()
            case.failure(let error):
                print("Not Success",error)
            }

        }


    }

for display to tableview

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       if tableView == tableview1{
        return fees.count
        }else{
        return descriptionFe.count
        }

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

        if tableView == tableview1{
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! FeesTableViewCell
        let getdata = fees[indexPath.row]
        cell.billno_txt.text = getdata.Billno

            cell.received_date_txt.text = getdata.recivedDate


            cell.status_txt.text = getdata.status
            cell.total_amount_txt.text = getdata.AmountPaid


            cell.date_txt.text = getdata.recivedDate




        return cell

        }
        else{
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! FeesTableViewCell
//           // let getdatafees = fees[indexPath.row]

          let getdatadesciption = self.descriptionFe[indexPath.row]


            cell.inner_txt1.text = getdatadesciption.des

           // cell.inner_txt2.text = fees[indexPath.row].AmountPaid


            return cell
        }
    }

and JSON api data format

[{
        "StdID": 95,
        "Status": "D",
        "NAME": "Calvin Patterson",
        "CLASSNO": "1",
        "recivedDate": "2017-06-08T00:00:00",
        "MasterID": "E0017",
        "RecieptNo": 83,
        "DateOfReciept": "2017-06-08T00:00:00",
        "Description": "[{\"des\":\"Admission\",\"Amount\":1200},{\"des\":\"Due\",\"Amount\":0}]",
        "AmountPaid": 1200,
        "dueDate": "2017-06-29T00:00:00",
        "dueAmount": 1200,
        "reciever": "Mr. Adminstrator",
        "CLASS_ID": 2021,
        "receivedAmount": 0
    },
    {
        "StdID": 95,
        "Status": "P",
        "NAME": "Calvin Patterson",
        "CLASSNO": "1",
        "recivedDate": "2017-07-13T00:00:00",
        "MasterID": "E0017",
        "RecieptNo": 1171,
        "DateOfReciept": "2017-07-01T00:00:00",
        "Description": "[{\"des\":\"Admission Fee\",\"Amount\":2000},{\"des\":\"Due\",\"Amount\":1200}]",
        "AmountPaid": 3200,
        "dueDate": "2017-07-30T00:00:00",
        "dueAmount": 3200,
        "reciever": "Mr. Adminstrator",
        "CLASS_ID": 2021,
        "receivedAmount": 0
    }]

enter image description here

I am confused to display that description in tableview , how it can be displayed data of description datas ??

14
  • What does your UI look like? Inside the cell how do you want to show description. At the moment from your code it looks like you are using 2 tableviews, but then both the views are reusing the same tableviewcell and the properties being used are different in each cell so i am a bit confused Commented Jul 17, 2017 at 5:45
  • @Bikesh Your Description is coming as a string and not as an array. You might have to apply some logic to parse it. When you are reloading your tableview, do you see your description populated in your object - descriptionFe Commented Jul 17, 2017 at 5:52
  • i have updated my design and in tableview cell i have another tableview and called this inner table view in FeesTableViewCell Commented Jul 17, 2017 at 5:55
  • i have done this same thing but in parsing data to another viewcontroller but here i need is to display both fees and description data in same view controller or i'am doing wrong in this way @kapsym Commented Jul 17, 2017 at 5:58
  • Check for multiple things here as i am not 100% sure at the moment what is wrong. 1) See if your descriptionFe is populated after you have parsed the data and before showing on UI 2) See the identifier of your tableviewcell inside your primary cell. Right now in both tableviews you are using same identifier which inwithIdentifier: "cell" but the properties of both cells are looking different. Check and revert, I am online Commented Jul 17, 2017 at 6:15

1 Answer 1

1

Basically there is nothing wrong in your code at all, But what you think and what data actually has is the main conflict, let me tell you where is the exact problem making you crazy.

Clearly observe Your json response. I have edited first record in your data and does not changed anything to the second record. Observe your Description value in 2 record.

[{
    "StdID": 95,
    "Status": "D",
    "NAME": "Calvin Patterson",
    "CLASSNO": "1",
    "recivedDate": "2017-06-08T00:00:00",
    "MasterID": "E0017",
    "RecieptNo": 83,
    "DateOfReciept": "2017-06-08T00:00:00",
    "Description": [{"des":"Admission","Amount":1200},{"des":"Due","Amount":0}],
    "AmountPaid": 1200,
    "dueDate": "2017-06-29T00:00:00",
    "dueAmount": 1200,
    "reciever": "Mr. Adminstrator",
    "CLASS_ID": 2021,
    "receivedAmount": 0
},
{
    "StdID": 95,
    "Status": "P",
    "NAME": "Calvin Patterson",
    "CLASSNO": "1",
    "recivedDate": "2017-07-13T00:00:00",
    "MasterID": "E0017",
    "RecieptNo": 1171,
    "DateOfReciept": "2017-07-01T00:00:00",
    "Description": "[{\"des\":\"Admission Fee\",\"Amount\":2000},{\"des\":\"Due\",\"Amount\":1200}]",
    "AmountPaid": 3200,
    "dueDate": "2017-07-30T00:00:00",
    "dueAmount": 3200,
    "reciever": "Mr. Adminstrator",
    "CLASS_ID": 2021,
    "receivedAmount": 0
}]

What i have done in your first record is.

This is the actual data:

 "Description": "[{\"des\":\"Admission\",\"Amount\":1200},{\"des\":\"Due\",\"Amount\":0}]"

Resultant data after i removed quotation marks and backward slashes.

"Description": [{"des":"Admission","Amount":1200},{"des":"Due","Amount":0}]

That's it if you run your code for this type of data then you get what you want. What you are mistake is you are accessing data from a string value, what i mean is

   "Description": "[{\"des\":\"Admission Fee\",\"Amount\":2000},{\"des\":\"Due\",\"Amount\":1200}]",

in the above data the value of your description key is a string type, but you are assuming it as an array of dictionaries and accessing those records that is the mistake. To solve this just make sure your data should be like as in below format.

 [{
    "StdID": 95,
    "Status": "D",
    "NAME": "Calvin Patterson",
    "CLASSNO": "1",
    "recivedDate": "2017-06-08T00:00:00",
    "MasterID": "E0017",
    "RecieptNo": 83,
    "DateOfReciept": "2017-06-08T00:00:00",
    "Description": [{"des":"Admission","Amount":1200},{"des":"Due","Amount":0}],
    "AmountPaid": 1200,
    "dueDate": "2017-06-29T00:00:00",
    "dueAmount": 1200,
    "reciever": "Mr. Adminstrator",
    "CLASS_ID": 2021,
    "receivedAmount": 0
},
{
    "StdID": 95,
    "Status": "P",
    "NAME": "Calvin Patterson",
    "CLASSNO": "1",
    "recivedDate": "2017-07-13T00:00:00",
    "MasterID": "E0017",
    "RecieptNo": 1171,
    "DateOfReciept": "2017-07-01T00:00:00",
    "Description": [{"des":"Admission Fee","Amount":2000},{"des":"Due","Amount":1200}],
    "AmountPaid": 3200,
    "dueDate": "2017-07-30T00:00:00",
    "dueAmount": 3200,
    "reciever": "Mr. Adminstrator",
    "CLASS_ID": 2021,
    "receivedAmount": 0
}]
Sign up to request clarification or add additional context in comments.

10 Comments

I was also assuming JSON format was incorrect n how will populate this description in tableview will u help me
and how can we remove that backward slash from jsondata
Manual conversion is not good, contact your api author to change response like that.
ok will ask author to change it but how to display description in same viewcontroller tableview
you already done it in else part of the table views cell for row at index path delegate method. Just make correct json response and run your code then you will get what you are expecting.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.