Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UICollectionView未显示正确数量的项目_Ios_Swift_Xcode - Fatal编程技术网

Ios UICollectionView未显示正确数量的项目

Ios UICollectionView未显示正确数量的项目,ios,swift,xcode,Ios,Swift,Xcode,我正在制作一个日历应用程序,当然,我希望一周有7天。这是一周中5天的当前显示方式: 这是我的故事板。我已经确保有足够的空间容纳7件物品,以符合我的要求。与我的问题相关的一件事是,当我添加更多的项目(单元格)时,它们与第一个项目的标识符(日历)不同 下面是我的视图控制器代码: import UIKit class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource{ @I

我正在制作一个日历应用程序,当然,我希望一周有7天。这是一周中5天的当前显示方式:

这是我的故事板。我已经确保有足够的空间容纳7件物品,以符合我的要求。与我的问题相关的一件事是,当我添加更多的项目(单元格)时,它们与第一个项目的标识符(日历)不同

下面是我的视图控制器代码:

import UIKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource{

@IBOutlet weak var Calendar: UICollectionView!
@IBOutlet weak var MonthLabel: UILabel!

let Months = ["January","February","March","April","May","June","July",
              "August","September","October","November","December"]

let DaysOfMonth = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday","Sunday"]

let DaysInMonths = [31,28,31,30,31,30,31,31,30,31,30,31]

var currentMonth = String()

override func viewDidLoad() {
    super.viewDidLoad()
    
    currentMonth = Months[month]
    
    MonthLabel.text = "\(currentMonth) \(year)"
    
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return DaysInMonths[month]
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Calendar", for: indexPath) as! DateCollectionViewCell
    cell.backgroundColor = UIColor.clear
    cell.DateLabel.text = "\(indexPath.row + 1)"
    
    return cell
}

}

总而言之:我希望在我的集合视图中一行有7个项目,但目前有5个。我已尝试更改情节提要中的间距以及集合视图单元格的标识符。

尝试在indexPath处指定项目的大小,以确保已设置
self.calendar.delegate=self
,然后向
视图控制器添加扩展名

extension ViewController: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: self.calendar.bounds.size.width / 7, height: your_cell_height)
    }
}
假设您没有指定单元格之间的任何间距,这将起作用。如果您指定了单元格之间的间距,请确保在计算中也考虑到该间距

例如:如果希望单元格之间的间距(水平方向)为
10
计算将更改为

(self.calendar.bounds.size.width/7)-60)
为什么是60?(7个单元之间的6个空间,每个单元的大小为10 so 6*10)

最后,为什么它在XIB中显示7个单元而不是在真实设备/模拟器中?XIB的屏幕宽度可能与设备屏幕宽度不同,您可以将XIB拖动到所需的宽度,以水平容纳您想要的任意多个单元格,但这并不保证应用程序在真实设备中运行时会获得类似的真实属性

这是一个流动布局,所以内容会流动,当它意识到一行中没有更多的空间容纳更多的单元格时,内容(单元格)会流动到下一行,希望澄清问题

p.S:千万不要用Pascal大小写来命名变量(第一个字母是大写)示例
@IBOutlet弱var日历:UICollectionView始终使用骆驼壳将其更改为
@ibvar日历:UICollectionView