![](/img/trans.png)
[英]Displaying repeating elements from json array only once in table view swift
[英]array data is repeating in table view in swift 4
這些是一些變量,我想在表視圖或集合視圖中顯示哪些數據。
let inr = ["200.0", "1200.0", "500.0", "2000.0", "2000.0", "2000.0", "2000.0", "2000.0", "3000.0", "5000.0", "1000.0", "100000.0", "100000.0"]
let reqtime = ["2018-04-11 13:54:46", "2018-04-09 16:31:42", "2018-04-09 12:26:32", "2018-04-09 12:19:40", "2018-04-09 12:19:35", "2018-04-09 12:19:23", "2018-04-09 12:09:05", "2018-04-09 12:08:25", "2018-04-09 11:38:15", "2018-04-09 11:07:59", "2018-04-09 11:03:58", "2018-04-09 10:54:55", "2018-04-09 10:45:47"]
let status = ["Request Approve", "Request Pending", "Request Pending", "Request Pending", "Request Pending", "Request Approve", "Request Pending", "Request Pending", "Request Pending", "Request Pending", "Request Pending", "Request Pending", "Request Approve"]
這是我的表格視圖代理功能代碼:
func numberOfSections(in tableView: UITableView) -> Int {
//print("inrvalue sec\(self.inrValues.count)")
return inr.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
//print("section p\(self.requestTimeValue[section])")
return reqtime[section]
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 44.0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableViewOutlet.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! DepositHistoryTableViewCell
let inrval = Double(inr[indexPath.section])
cell.lblINR.text = String(inrval)
cell.lblStatus.text = status[indexPath.section]
return cell
}
數據顯示如下圖: 表視圖數據
問題1.想要顯示這樣的數據...
Section: reqtime(row[0])
inr(row[0])
status(row[0]),
Section: reqtime(row[1])
inr(row[1])
status(row[1])
Section: reqtime(row[2])
inr(row[2])
status(row[2])
等等。
問題2:是否有任何其他的方式來顯示這樣更好的方式這樣的數據: 任何其他方式在SWIFT這樣的數據
更改我的代碼后,數據如下圖所示: 這是numberofRowinSection之后顯示的數據如何返回1
在這里,您在
inr
,reqtime
和status
沒有相同的項目reqtime
,因此對於每個inr
項目,您將只有一個reqtime
和single
狀態:
將您的numberOfRowsInSection
更新為:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
注意:當前,您正在此方法中返回reqtime[section]
(字符串) ,這也不正確,因為此函數只能具有Int
返回值。
將UITableView的委托和數據源方法更新為:
func numberOfSections(in tableView: UITableView) -> Int {
return inr.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return reqtime[section]
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Here you have same no of items in inr, reqtime and status so for each inr item you will only have a single reqtime and single status
return 1
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 60.0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableViewOutlet.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! DepositHistoryTableViewCell
let inrval = Double(inr[indexPath.section])
cell.lblINR.text = String(format: "%.2f",inrval)
cell.lblStatus.text = status[indexPath.section]
return cell
}
更新: //如果您希望像水平表一樣顯示數據
let inr = ["200.0", "1200.0", "500.0", "2000.0", "2000.0", "2000.0", "2000.0", "2000.0", "3000.0", "5000.0", "1000.0", "100000.0", "100000.0"]
let reqtime = ["2018-04-11 13:54:46", "2018-04-09 16:31:42", "2018-04-09 12:26:32", "2018-04-09 12:19:40", "2018-04-09 12:19:35", "2018-04-09 12:19:23", "2018-04-09 12:09:05", "2018-04-09 12:08:25", "2018-04-09 11:38:15", "2018-04-09 11:07:59", "2018-04-09 11:03:58", "2018-04-09 10:54:55", "2018-04-09 10:45:47"]
let status = ["Approve", "Pending", "Pending", "Pending", "Pending", "Approve", "Pending", "Pending", "Pending", "Pending", "Pending", "Pending", "Approve"]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return inr.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 40.0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableViewOutlet.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! DepositHistoryTableViewCell
cell.lblRequestTime.text = reqtime[indexPath.row]
let inrval = Double(inr[indexPath.row])
cell.lblINR.text = String(format: "%.2f",inrval!)
cell.lblStatus.text = status[indexPath.row]
return cell
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.