访问集合视图中的数组索引数组,Swift。

huangapple go评论61阅读模式
英文:

Access Array of Array Index in Collectionview swift

问题

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 combine multiple arrays into a single array, but I got the error when I accessed the index value as 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 haven't had any results yet. Please correct me if I'm doing it wrong.

Any help would be greatly appreciated.

英文:

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

答案1

得分: 1

You are creating an array of arrays using the following code:

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

What you should do is as follows:

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

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

The output will be:

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

Now, you can use it in a cell as follows:

cell.txtHeading.text = collectionArrayStr[indexPath.row]
英文:

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]

答案2

得分: 0

好像你调用了 indexPath.row 两次?

所以你告诉 collectionView 有多少个 sections 和每个 section 中有多少行。这意味着 indexPath.section 和 indexPath.row。

    if let indexPathSlctedImage = collectionArrayStr[indexPath.section] as? [String] {
        cell.txtHeading.text = indexPathSlctedImage[indexPath.row] 
    }

<details>
<summary>英文:</summary>

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

huangapple
  • 本文由 发表于 2023年5月22日 16:02:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76304124.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定