user6702783
user6702783

Reputation: 223

Swift Tableview

I have an array[13, 30, 15]. I want to show them in a tableview, just want to give different numbers with different string title showing in the tableview. Like:

Apple: 13
Banana: 30
Pear: 15

Are there any simple ways to realize it? Any help appreciated.

Upvotes: 0

Views: 192

Answers (4)

Salman Ghumsani
Salman Ghumsani

Reputation: 3657

So You have an array of both the values, then you can use following:-

arrayInt[13, 30, 15]
arrayString["Apple","Banana","Pear"]
cell.label.text = "\(arrayString[indexPath.row]): \(arrayInt[indexPath.row])"

Output becomes:-

Apple: 13
Banana: 30
Pear: 15

Also, if you have just an array of int-values and stuck to show up then you could use:-

cell.label.text = "Your int values : \(arrayInt[indexPath.row])"

If you stuck with dynamic data then use following to append the data:-

arrayInt.append(newInt)
arrayString.append(newString)

Upvotes: 2

KRUNAL
KRUNAL

Reputation: 1

Use two arrays

var arraynumbers = [13,30,15] var arraynames = [name1,name2,name3]

var string = "(arraynames[indexPath.row]): (arraynumbers[indexPath.row])"

cell.textlabel.text = string

Upvotes: 0

Mark
Mark

Reputation: 1

  1. Use Associative Array.
  2. Pass the string(Apple) as Key & Value() As Index

Upvotes: 0

Nirav D
Nirav D

Reputation: 72410

Just create one Dictionary like this [Int : String] and use this inside your cellForRowAtIndexPath.

var dict = [13 :"Apple", 30 : "Banana", 15 : "Pear"]

Now in cellForRowAtIndexPath

cell.label.text = dict[yourIntArray[indexPath.row]] 

Edit:

As you mention in comment your have only 3 value and its order never change then make one string array in order that you want and use that array in cellForRowAtIndexPath

var stringArr = ["Apple","Banana","Pear"]

Now in cellForRowAtIndexPath

cell.label.text = stringArr[indexPath.row]

Upvotes: 0

Related Questions