数组数据在swift 4的表视图中重复

时间:2018-04-13 08:48:20

标签: ios arrays swift uitableview uicollectionview

这些是一些变量,我希望在表视图或集合视图中显示哪些数据。

   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
}

如下图所示的数据: table view data

问题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:有没有其他方法可以更好地显示这些数据:any other way to data like this in swift

更改我的代码后,数据如下图所示:this is how data shown after numberofRowinSection return 1

1 个答案:

答案 0 :(得分:1)

  

此处inrreqtimestatus中的项目相同,因此对于每个inr项目,您只有一个reqtime和{ {1}}状态:

将您的single更新为:

numberOfRowsInSection

注意:目前您在此方法中返回func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 1 } (字符串),这也不正确,因为此函数只能有{{1}返回值。

enter image description here

将UITableView的委托和数据源方法更新为:

reqtime[section]

<强>更新 //如果您希望显示类似水平表的数据

Int

enter image description here