-3

I have 4 different arrays:

var arrayFirst = ["1"]
var arraySecond = ["2"]
var arrayThird = ["3"]
var arrayFourth = ["4"]

Created a Single Array for collection view.

 var collectionArrayStr = [arrayFirst,arraySecond,arrayThird,arrayFourth]

 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return collectionArrayStr.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ViewCell", for: indexPath) as! ViewCell

    print(collectionArrayStr[indexPath.row])
    //output:- (["1"])

    if let indexPathSlctedImage = collectionArrayStr[indexPath.row] as? [String] {
        cell.txtHeading.text = indexPathSlctedImage[indexPath.row] // No exact matches in call to subscript 
    }
    return cell
}

I followed the above approach to multiple arrays into a single array but I got the error when I access the index value I got the error given below:-

No exact matches in call to subscript

Question: How to get an index value in a single array?

Can someone please explain to me how to do this, I've tried with the above code but have no results yet. Please Correct me if I'm doing wrong.

Any help would be greatly appreciated

Sham Dhiman
  • 1,348
  • 1
  • 21
  • 59
  • Are `arrayFirst` etc declared at the same level as `collectionArrayStr`? Are they all properties of a class? If so, you should also get the error: "cannot use instance member 'arrayFirst' within property initializer; property initializers run before 'self' is available". – Sweeper May 22 '23 at 08:09
  • @ShehataGamal, sure I will post give me some time, please. – Sham Dhiman May 22 '23 at 08:19
  • @Sweeper, I got this error when I access the array index. No exact matches in call to subscript – Sham Dhiman May 22 '23 at 08:19
  • 1
    You are creating an array of arrays but what you need to do is to create a flat array containing all the elements of the other arrays. Try using `+` – Joakim Danielson May 22 '23 at 08:24

2 Answers2

1

You are creating array of array by using

var collectionArrayStr = [arrayFirst,arraySecond,arrayThird,arrayFourth]

What you should do is as below.

var collectionArrayStr : [String] = []
collectionArrayStr.append(contentsOf: arrayFirst)
collectionArrayStr.append(contentsOf: arraySecond)
collectionArrayStr.append(contentsOf: arrayThird)
collectionArrayStr.append(contentsOf: arrayFourth)

print("collectionArrayStr==\(collectionArrayStr)")

Output will be

collectionArrayStr==["1", "2", "3", "4"]

Now in cell use as below.

cell.txtHeading.text = collectionArrayStr[indexPath.row]
Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
0

It looks like you are calling indexPath.row twice?

So you tell the collectionView the amount of sections and amount of rows in section. That means indexPath.section and indexPath.row.

    if let indexPathSlctedImage = collectionArrayStr[indexPath.section] as? [String] {
        cell.txtHeading.text = indexPathSlctedImage[indexPath.row] 
    }
Vollan
  • 1,887
  • 11
  • 26
  • Let me check with your code. I will update you shortly. – Sham Dhiman May 22 '23 at 08:18
  • 1
    Despite the two dimensional array, it is not clear if the OP wants each of the arrays to be a separate section. The code uses the default of 1 section since it doesn’t implement the delegate method for number of sections and is returning the number of arrays (4) for the row count of that section. But it is also using the row number as the index into the outer and inner array, so is going to crash as soon as it tries to show anything other than the first cell. – Geoff Hackworth May 22 '23 at 08:27
  • @GeoffHackworth `But it is also using the row number as the index into the outer and inner array, so is going to crash as soon as it tries to show anything other than the first cell.` Yeah, it was due to this i was thinking he was going to use multiple sections. Otherwise there are no evidence in his code either to why he would use nested arrays. – Vollan May 22 '23 at 08:31
  • @Vollan, I want only one section. I got the crash when I used your code. :( – Sham Dhiman May 22 '23 at 08:33
  • 1
    @ShamDhiman So then you only want 1 array? So can you flatmap the array? – Vollan May 22 '23 at 08:46
  • @Vollan, ok I will check with Flatmap. give me some time, please. – Sham Dhiman May 22 '23 at 09:01