[英]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.