KMC
KMC

Reputation: 1742

indexPath.row always returns zero

I have two collection view and one displays names and the other one displays age of the corresponding person. This data is stored inside array of dictionary in a form of "[["Name","Age"],["Name": "Daniel", "Age" : "20"],["Name":"Jake","Age":"20"]]. This data comes from CSV file, so the first element is a header. Inside collectionView cellForItemAtIndexPath, I'm checking collection view and provide data base on row number like cell[indexPath.row]["Name"] and cell2[indexPath.row]["Age"]. However, indexPath.row always returns zero, so I'm getting just headers -

enter image description here

How do fix this issue? This is my code -

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 2
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 1
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    if collectionView == self.nameCollectionView {
        let nameCell = collectionView.dequeueReusableCellWithReuseIdentifier("NameCell", forIndexPath: indexPath) as! NameCell

        nameCell.data.text = self.data?[indexPath.row]["Name"]
        println(indexPath.row)

        return nameCell
    }
    else{
        let ageCell = collectionView.dequeueReusableCellWithReuseIdentifier("AgeCell", forIndexPath: indexPath) as! AgeCell

        ageCell.data.text = self.data?[indexPath.row]["Age"]



        return ageCell
    }

}

Upvotes: 1

Views: 3668

Answers (3)

Saheb Roy
Saheb Roy

Reputation: 5957

IndexPath is a property which has the following structure Indexpath {Section, Row}. So if you want your data in two different section with a single row in them then indexpath.row for each of them is going to return 0 as because

For section index 0 - Indexpath[0,0] meaning indexpath of section index 0 and row index 0

For section index 1 - Indexpath[1,0] meaning indexpath of section index 1 and row index 0

Hope could make you understand.

Upvotes: 1

Nitin Gohel
Nitin Gohel

Reputation: 49710

As par your code you are setting numberOfItemsInSection only 1 then you always get 0th index. make there is dynamic value for example return Array.count.

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.data.count  // here you need to set dynamic count of array
}

UPDATE:

If you followed numberOfSectionsInCollectionView then make your code like following of cellForRow:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    if collectionView == self.nameCollectionView {
        let nameCell = collectionView.dequeueReusableCellWithReuseIdentifier("NameCell", forIndexPath: indexPath) as! NameCell

        nameCell.data.text = self.data?[indexPath.section]["Name"]
        println(indexPath.section)

        return nameCell
    }
    else{
        let ageCell = collectionView.dequeueReusableCellWithReuseIdentifier("AgeCell", forIndexPath: indexPath) as! AgeCell

        ageCell.data.text = self.data?[indexPath.section]["Age"]



        return ageCell
    }

}

Upvotes: 5

Duncan C
Duncan C

Reputation: 131398

As others have pointed out, you are telling your collection views that you always have 2 sections and 1 item in each section. Thus the collection view will only ever ask for 1 item in each section. Thus there will only ever BE one item in each section (index 0).

You say "This data is stored inside dictionary in a form..."

Is it a dictionary or an array of dictionaries? A dictionary is an unordered collection, so it is not appropriate for storing an ordered set of items for feeding to a collection view or table view. An array of dictionaries is appropriate. From the data you show, and your code, it looks like you have an array of dictionaries. You should edit your question to make that clear.

Your code doesn't really make sense. You have 2 different collection views, and you have each display different data. You tell your collection views that you have 2 sections but ignore the section number and create the same data for both sections. Something is wrong there.

Upvotes: 0

Related Questions