0

I am facing an issue in this since a long.

  • Problem:

I need to get value as per the index of following array in my tableView.

indexArray = [3, 6, 4, 5, 8, 30, 31, 7, 18, 29, 1]

paramNameArray = ["Date", "Sync Time", "UUC2T7", "Set MD", "Total KVA", "Total KW", "Running MD", "SMS 1 KW", "P.F.", "ET-5 KW", "ET-3_4 KW", "ET- 6_7 KW", "ABP-1 KW", "ABP-1A KW", "ABP-2 KW", "G-22 KW", "UUC36T37", "G-23 KW", "WRM KW", "Total KWH", "Total KVAH", "ET-5 KWH", "ET-3_4 KWH", "ET-6_7 KWH", "ABP-1 KWH", "ABP-1A KWH", "ABP-2 KWH", "SMS 1 KWH", "UUC60T73", "PLC Time", "Voltage", "Current", "Load Factor", "Avg. P.F."]

Problem:

  • Now in cellForRowAt indexPath of my tableView, I need "Set MD" value at indexPath 0 (need value as per the index of indexArray)

This way for each row of my tableView, I need value as per the indexArray's index.

For Example:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{
    (at 0th Index) cell.lblParameter.text = "Set MD"  // value from paramNameArray as per indexArray's index(3)

    (at 1st Index) cell.lblParameter.text = "Running MD"  // value from paramNameArray as per indexArray's index(6) 

    // And so on...

    return cell
}

Help would be much appreciated, Thanks! :)

7
  • 2
    cell.lblParameter.text = paramNameArray[indexArray[indexPath.row]] Commented Jun 22, 2018 at 12:48
  • 1
    @vacawama, I can't believe it was so simple. Thank you so much man! Commented Jun 22, 2018 at 12:51
  • 2
    You should rethink your data structures. This has a code smell and you're going to end up with a crashing app. Commented Jun 22, 2018 at 12:54
  • @AshleyMills, Sorry for my English. But I can't understand what you said. Commented Jun 22, 2018 at 12:56
  • 1
    Good luck with that! Commented Jun 22, 2018 at 13:09

1 Answer 1

2

First you need to get an index from the indexArray using index path in cellForRowAt: method. and then using that index (got from indexArray) you get value from paramNameArray.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let index = indexArray[indexPath.row] as! Int

//check that index won't exceeds `paramNameArray.count` otherwise it will throw `ArrayIndexOutOfBoundsException`.

  if index < paramNameArray.count {

    let text = paramNameArray[index]
    cell.lblParameter.text = text
  } else {
     cell.lblParameter.text = "some default text goes here in case if value is not present"
  }

  return cell
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.