0

I have single array which contain game`s informations. My Json has 12 items in a page. I did created 4 sections which has 3 rows. It is repeating first 3 items of array in every sections.

Screenshot from app

I want to use like that;

Total Items = 12

  1. Section = 1 2 3
  2. Section = 4 5 6
  3. Section = 7 8 9
  4. Section = 10 11 12

How can I do that ? Thanks in advance :)

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return id.count / 3
}


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

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

        cell.gameName.text = name[indexPath.row]
        cell.gameImage.sd_setImage(with: URL(string:resimUrl[indexPath.row]))

    return cell
}
3
  • You can maintain the dictionary like you have key value pairs. For example key should be 0,1 and 2 and value should be array of 3 elements each. So in number of sections you need to return dict.count and in number of rows in section you need to fetch the dict elements like dict[indexPath.row].values.count. Commented Feb 3, 2019 at 13:06
  • @RajatGupta thanks for suggest, I have to change api from server side for it but I thought there is an easy way for do that :/ Commented Feb 3, 2019 at 13:10
  • the data source is an array of objects right ? Commented Feb 3, 2019 at 13:14

2 Answers 2

1

I don't think it's such a good idea. I would rather create the section separately by making a section manager than making them from the same array. But, if you want to do it the way you are doing it right now. Here is an easy fix:

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return id.count / 3
}


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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "lastAddedCell", for: indexPath) as! lastAddedCell
    let index = indexPath.row + (indexPath.section * 3) // The index is then based on the section which is being presented
    cell.gameName.text = name[index]
    cell.gameImage.sd_setImage(with: URL(string:resimUrl[indexPath.row]))

    return cell
}
Sign up to request clarification or add additional context in comments.

Comments

0

Consider this case below and implement it,

var array = [1,2,3,4,5,6,7,8,9,10,11,12]

//Statcially you can slice them like this

    var arr2 = array[0...2] {
        didSet {
            //reload your collection view
        }
    }
    var arr3 = array[3...5]
    var arr4 = array[6...8]
    var arr5 = array[9...array.count - 1]

Above you manually sliced the dataSource for each UICollectionView but the problem is this is really risky and eventually can lead to an Index Out of Range crash, so we dynamically slice the array thru looping in it using the index of each element in range of +3 indexes to append to the new UICollectionView data source.

    // loop thru the main array and slice it based on indexes
for(index, number) in array.enumerated() {
    if 0...2 ~=  index { // if in range
        arr2.append(number)
    } else
    if index <= 5 {
        arr3.append(number)
    } else
    if index <= 8 {
        arr4.append(number)
    } else
    if index <= 11 {
        arr5.append(number)
    }
}

Finally : in your numberOfItemsInSection check the UICollectionView and set return its data source like,

if collectionView = myMainCollectionView { 
return arr3.count 
}

And goes the same for the cellForItemAt

Heads Up : make sure your dataSource arrays are empty initially,

let arr2: [Int] = [] { 
 didSet{
  //reload your collectionView 
 }
}

2 Comments

thanks for answer bro, I did solved it thanks to Galo Torres Sevilla`s answer
yes ill keep it for the additional info might help someone

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.