我已經在tableview中顯示了第一個json數據,但是當試圖顯示內部數組數據的數組數據時,它在表視圖上顯示空白數據,超出範圍不知道我在哪裏出錯或忘記編寫代碼,我可以在表格視圖中顯示費用類數據,但無法顯示描述數據,或者是否需要更改我的用戶設計顯示使用Alamofire從JSON數據到tableview的嵌套數組
這裏我有
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
}
}
模型類代碼用於獲取JSON數據
@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)
}
}
}
用於顯示給的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
}
}
和JSON API的數據格式
[{
"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
}]
我很困惑,以顯示在tableview中這樣的描述,它可以如何被顯示的數據描述數據?
你的用戶界面是什麼樣的?在單元格內部,您想如何顯示說明。從你的代碼看起來你使用了2個tableviews,但是這兩個視圖都重複使用了相同的tableviewcell,並且每個單元格的屬性都不相同,所以我有點困惑 –
@Bikesh Your Description is coming as a字符串而不是數組。您可能需要應用一些邏輯來解析它。當你重新加載你的tableview,你是否看到你的描述填充在你的對象 - descriptionFe –
我已經更新了我的設計,並在tableview單元格中我有另一個tableview,並在FeesTableViewCell中調用了這個內部表格視圖 –