【发布时间】:2017-12-21 12:56:21
【问题描述】:
我在 tableview 中显示了第一个 json 数据,但是当尝试显示内部数组数据的数组数据时,它在 table view 上显示空白数据,我已经厌倦了很多方式,有时它向我显示索引超出范围不知道我在哪里出错或忘记编写代码,我可以在表格视图中显示该费用类数据但无法显示描述数据,或者我需要更改我的 Ui 设计
这里我有模型类
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 个 tableview,但是两个视图都在重用同一个 tableviewcell,并且每个单元格中使用的属性都不同,所以我有点困惑
-
@Bikesh 您的描述是以字符串而不是数组的形式出现的。您可能必须应用一些逻辑来解析它。当您重新加载表格视图时,您是否看到您的描述填充在您的对象中 - descriptionFe
-
我已经更新了我的设计,并且在 tableview 单元格中我有另一个 tableview 并在 FeesTableViewCell 中调用了这个内部表格视图
-
我做了同样的事情,但是在将数据解析到另一个视图控制器时,但这里我需要在同一个视图控制器中显示费用和描述数据,或者我以这种方式做错了@kapsym跨度>
-
在这里检查多项内容,因为目前我不能 100% 确定出了什么问题。 1) 查看您的 descriptionFe 在您解析数据后并在 UI 上显示之前是否已填充 2) 查看您的主单元格内的 tableviewcell 的标识符。现在在两个表格视图中,您都使用相同的标识符,其中 inwithIdentifier: "cell" 但两个单元格的属性看起来不同。检查并还原,我在线
标签: ios uitableview swift3 alamofire swifty-json