UICollectionView没有显示正确的项目数

伊莱·古奇(Eli Gooch)

我正在制作日历应用程序,自然,我希望一周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个。我试图更改情节提要中的间距以及收藏视图单元格的标识符。

桑迪普·班达里(Sandeep Bhandari)

Try specifying the size for item at indexPath, to do that make sure you have set self.calendar.delegate = self and then add an extension to your ViewController

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)
    }
}

This will work assuming you haven't specified any spacing between the cells, if you have specified spacing between the cells make sure you account it as well in your calculation.

For example: if you want spacing between cells (horizontally) be 10 calculation will change to

(self.calendar.bounds.size.width / 7) - 60) why 60? ( 6 spaces between 7 cells each of size 10 so 6 * 10)

Finally, why is it showing 7 cells in XIB and not in real device/ simulator? XIB's screen width might be different from the device screen width, you can drag Xib to required width to accommodate as many cells as you want horizontally, that does not guarantee that when app runs in real device it gets the similar real estate.

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

PS:切勿使用Pascal大小写(第一个字母为大写)来命名变量,请@IBOutlet weak var Calendar: UICollectionView!始终使用Camel大小写更改为@IBOutlet weak var calendar: UICollectionView!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在 UICollectionView 中查找每行实际显示的项目数

问题在keyup搜索输入(分页)上显示正确的项目数

待办事项列表-“正确的细节”单元格样式以显示项目数

List Section 根据项目数显示

无效的更新:UICollectionView上的项目数无效

所选项目ComboBox没有显示正确的ID WPF

UICollectionView:在没有任何项目的部分中显示标签“无项目”

有没有办法在命令中放置“X”个参数,具体取决于列表中的项目数?

显示子类别和主要类别下的项目数

如何限制Django应用中显示的项目数

限制要在反应预测输入中显示的项目数

Angular ui grid pagination - 页面显示中的项目数

遍历对象数组,隐藏重复项并显示项目数?

Flutter:如何显示列表中的项目数

仅显示适合的项目数,然后展开以适合

限制WPF ListView中显示的项目数

如何在PHP中显示数组中的项目数

父类别中未显示项目数

如果项目数大于 4,Wordpress 会显示简码

显示对象数组中的项目数,无重复

显示PHP中每个类别的项目数

在Java数组中显示的项目数量有限

斯威夫特| 没有情节提要的UICollectionView显示不正确的数据

使用 LINQ 返回具有最低项目数的 ListBox

涵盖所有情况的最少项目数

获取数组中具有某些属性的项目数

转换具有连续项目数的新列

如何选择要使用UICollectionView预取的项目数?

反应:仅显示完全适合弹性框的项目,并动态确定要显示的项目数