英文:
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]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论