简体   繁体   English

如何从特定索引路径加载集合视图单元格?

[英]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.如果 01-02-207 星期几从 3 开始,我需要从集合视图的第 3 个位置加载单元格。 If I get 6 I should load from the 6th position.如果我得到 6,我应该从第 6 个位置加载。 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)
}

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.我不知道您从哪里获取数据,是查询某个 API 还是您在某处有一个数组,但下面的内容应该可以帮助您。 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
}


}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在索引path.item-集合视图中的1中获取单元格 - how to get the cell at index path.item - 1 in collection view 如何查找选定集合视图单元格的索引路径? - How to find index Path for a selected Collection View Cell? 如何在swift中同时显示表格视图单元格索引路径和集合视图索引路径? - How to show table view cell index path and collection View Index path at the same time in swift? 如何在iOS Swift中设置特定索引的集合视图单元格? - How to set collection view cell of specific index in iOS Swift? 如何在单元格中为集合视图的索引路径功能调用图像URL和图像视图 - How to call image urls and image view in cell for index path function for a collection view 是否可以在 swift 中显示表格视图单元格索引路径和集合视图索引路径? - Is it possible to show table view cell index path and collection View Index path in swift? 如何根据“收藏”标识从“收藏”视图单元中选择到特定视图控制器 - How to Segue to a specific view controller depending on the Segue identity, from a Collection view cell 更改 collectionView 单元格下方的标题和描述 当集合视图单元格更改时。 使用索引路径执行此操作不起作用 - Changing the title and the description below the collectionView cell When the collection view cell change. Doing this with index path is not working 如何从集合视图中删除单元格? - How to delete a Cell from Collection View? iOS集合查看来自自定义单元类的加载图像 - IOS Collection View load Image from custom cell class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM