RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1165527
Accepted
Kirzzz
Kirzzz
Asked:2020-08-13 20:57:09 +0000 UTC2020-08-13 20:57:09 +0000 UTC 2020-08-13 20:57:09 +0000 UTC

为什么 CollectionView 不工作?

  • 772

我有一个带有显示时间的按钮的 CollectionView(可单击)。

这是我的代码:

import UIKit

class CreateWindow: UIViewController, UICollectionViewDelegateFlowLayout {
    
    let reuseIdentifier = "cell" // also enter this string as the cell identifier in the storyboard
    var items = ["11:30","12:30","13:30","14:30","15:30","16:30"]

    @IBOutlet weak var collectionView: UICollectionView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView.delegate = self
        self.collectionView.dataSource = self
    }
    
}

extension CreateWindow: UICollectionViewDataSource, UICollectionViewDelegate{
    
    // MARK: - UICollectionViewDataSource protocol
    
    // tell the collection view how many cells to make
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        print(self.items.count)
        return self.items.count
    }
    
    // make a cell for each cell index path
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        print(self.items.count)
        // get a reference to our storyboard cell
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! TimeCollection
        
        // Use the outlet in our custom class to get a reference to the UILabel in the cell
        cell.TimeLabel.text = self.items[indexPath.item]
        cell.backgroundColor = UIColor.cyan // make cell more visible in our example project
        
        return cell
    }
    
    // MARK: - UICollectionViewDelegate protocol
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        // handle tap events
        print("You selected cell #\(indexPath.item)!")
    }
}

class TimeCollection: UICollectionViewCell {
    
    @IBOutlet weak var TimeLabel: UILabel!
    
}

这是我的故事板: 故事板

collectionView.delegate我尝试通过 swift 代码和故事板本身绑定到ViewController. 启动时,只有单元格数量功能起作用,但创建它们的功能不起作用(print不显示任何内容)有谁知道如何解决这个问题?


附加信息:

我的图层的屏幕截图(集合为空) 在此处输入图像描述

单元类声明 在此处输入图像描述

集合视图依赖项 在此处输入图像描述

单元依赖

在此处输入图像描述

ios
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    schmidt9
    2020-08-13T21:45:53Z2020-08-13T21:45:53Z

    最有可能的是,您没有在情节提要中指定单元格的自定义类 - 您需要让集合视图清楚地为单元格使用哪个类。之后,@IBOutlet weak var TimeLabel: UILabel!与情节提要中单元格中的标签连接,一切正常

    在此处输入图像描述

    添加

    这是应该怎么做的

    在此处输入图像描述

    补充 2

    这就是调试视图层次结构中的样子

    在此处输入图像描述

    补充 3

    查看情节提要的源代码(Open As - Source Code),看看你的单元格类是否存在customClass="TimeCollection"。有时您仍然需要在 Custom Class 输入字段中按 Enter 来安装它。

    在此处输入图像描述

    补充 4

    一般来说,如果不起作用,你可以尝试将单元格移动到单独的xib(一般情况下更好),然后它需要注册,否则一切都一样(我命名的类TimeCollectionViewCell)

    self.collectionView.delegate = self
    self.collectionView.dataSource = self
    self.collectionView.register(UINib.init(nibName: "TimeCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "cell")
    
    • 1

相关问题

  • UIImageView 相对于 superview 缩放后的位置

  • 在密码中授权 type basic 时,符号#swift 4 被错误传输到服务器

  • 将 AppDelegate.h 添加到项目中

  • 调用共享表时出错

  • 接近传感器模拟

  • 帮助 ios 应用程序架构 (MVP)

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何从列表中打印最大元素(str 类型)的长度?

    • 2 个回答
  • Marko Smith

    如何在 PyQT5 中清除 QFrame 的内容

    • 1 个回答
  • Marko Smith

    如何将具有特定字符的字符串拆分为两个不同的列表?

    • 2 个回答
  • Marko Smith

    导航栏活动元素

    • 1 个回答
  • Marko Smith

    是否可以将文本放入数组中?[关闭]

    • 1 个回答
  • Marko Smith

    如何一次用多个分隔符拆分字符串?

    • 1 个回答
  • Marko Smith

    如何通过 ClassPath 创建 InputStream?

    • 2 个回答
  • Marko Smith

    在一个查询中连接多个表

    • 1 个回答
  • Marko Smith

    对列表列表中的所有值求和

    • 3 个回答
  • Marko Smith

    如何对齐 string.Format 中的列?

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5