简体   繁体   中英

array data is repeating in table view in swift 4

these are some variable , which data I want to show in table view or collection view.

   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"]

This is my table view Delegate Function Code:

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
}

Data shown like this image: table view data

Question 1. want to show data like this...

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])

and so on.

question 2: is there any other way to show this data in better way like this: any other way to data like this in swift

after change my code, data is showing like this in image below: this is how data shown after numberofRowinSection return 1

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:

Update your numberOfRowsInSection as:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

Note: Currently you are returning reqtime[section] (String) in this method which is also not right as this function can only have a Int return value.

在此处输入图片说明

Update UITableView's delegate and datasource methods as:

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
}

Update: // If you wish to show data like a horizontal table

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
}

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM