jerfin
jerfin

Reputation: 893

How to load collection view cell from a specific index path?

I am working on designing a calendar using a collection view. I am getting the days of week for each month. If for 01-02-207 the day of week start from 3, I need to load the cell from the 3rd position of the collection view. If I get 6 I should load from the 6th position. Can any one help?

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let s = CGSize(width: CGFloat(UIScreen.main.bounds.size.width / 7), height: CGFloat(UIScreen.main.bounds.size.height / 7))
    return s
}

//UICollectionViewDatasource methods
func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

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

    return numDays
}

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


    let myString = String(yourArray[indexPath.row])

    cell.day_lbl.text = myString
    cell.backgroundColor = self.randomColor()
    return cell
}

// custom function to generate a random UIColor
func randomColor() -> UIColor{
    let red = CGFloat(drand48())
    let green = CGFloat(drand48())
    let blue = CGFloat(drand48())
    return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
}

Upvotes: 1

Views: 981

Answers (1)

Pippo
Pippo

Reputation: 1473

You could use a struct to hold all the info you require foreach calendar month. I don't know where your getting your data from, wether is a query of some API or you have an array somewhere but the below should get you going. Mind you theres probably better way of going about this.

import UIKit

struct MonthStruct {
    var monthSequence: Int // this will help sort the array
    var month : String
    var numberOfDays : Int
    var startIndex : Int  // or startDay if using days
}

class MyClass: UIViewController {

// make an array from the struct items

var monthArray = [MonthStruct]()

override func viewDidLoad() {

    for months in year { // year being your data source from query or array????

        // hardcoding values to demonstrate
        monthArray.append(MonthStruct(monthSequence: 1,         // : month[0]   // : month.object.objectForKey.....
                                      month: "Janruary",        // : month[1]
                                      numberOfDays: 31,         // : month[2]
                                      startIndex: 3)            // : month[3]
        )

    }

}



func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    // [0] or other depending on month you want to display
    return monthArray[0].numberOfDays
}

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

   // this will just show stuff for cells after Tuesday
    if indexPath.item >= monthArray[0].startIndex {
        cell.day_lbl.text = myString
        cell.backgroundColor = self.randomColor()
    }

    return cell
}


}

Upvotes: 1

Related Questions