【问题标题】:Collection View Cells not appearing集合视图单元格未出现
【发布时间】:2016-02-26 04:29:51
【问题描述】:

我想显示尽可能多的collectionViewCells 和buttons,因为我的数组中有字符串。但是当我启动模拟器时,只有CollectionView的背景,但没有显示单元格。可能是什么错误?

这是我的 CollectionViewController 中的代码,我附加到 main.storyboard 中的 CollectionView:

class CollectionViewController: UICollectionViewController {

var Array = [String]()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    Array = ["1","2","3","4","5"]
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func collectionView(collectionView: UICollectionView, numberOfItemsSection section: Int) -> Int {
    return Array.count
}

override func
    collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        var cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! UICollectionViewCell

        var button = cell.viewWithTag(1) as! UIButton
        button.titleLabel?.text = Array[indexPath.row]

        return cell
}

}

这些是集合视图控制器的连接:

情节提要上的视图控制器:

【问题讨论】:

  • 如果您使用情节提要,这可能是一个自动布局问题。你能在 storyboard 上显示视图控制器的屏幕截图以及布局选项的屏幕截图吗?
  • 我尝试禁用自动布局,但仍然存在同样的问题。布局选项是什么意思?我在情节提要上添加了视图控制器的屏幕截图
  • 您是否记得将 StoryBoard 中控制器的自定义类设置为您的 CollectionViewController?
  • 您是否检查过您的“func collectionView(collectionView: UICollectionView, numberOfItemsSection section: Int)”是否正在执行?
  • 您的 UIViewController 是否在 UITabBarController 中?您能否检查一下这个问题是否与您的问题有关:stackoverflow.com/q/33177968/1171404

标签: ios xcode swift uicollectionviewcell collectionview


【解决方案1】:
func layoutCells() {
        let layout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 0, left: 10, bottom: 10, right: 10)
        layout.minimumInteritemSpacing = 5.0
        layout.minimumLineSpacing = 5.0
        layout.itemSize = CGSize(width: (UIScreen.mainScreen().bounds.size.width - 40)/3, height: ((UIScreen.mainScreen().bounds.size.width - 40)/3))
        collectionView!.collectionViewLayout = layout
    }

试试这个。从视图调用这个函数确实加载了。我认为问题在于您的收藏布局不正确。

func viewDidLoad() {
    layoutCells()
}

如果可行,您可以修改布局选项以满足您的需要。

【讨论】:

  • 遗憾的是仍然存在同样的问题。模拟器中只显示背景
  • 是显示单元格背景还是视图背景?
  • 像宝石一样工作!但是,对我来说,当我在 viewDidLoad() 方法中插入 layoutCells() 时,应用程序崩溃了,所以我把那部分省略了。
【解决方案2】:

您是否将CollectionViewController 设置为情节提要身份检查器? :)

在您更改viewDidLoad 方法中的数据后,我会尝试调用reloadData()。

希望有帮助

【讨论】:

  • 你能指定如何将 CollectionViewController 设置为故事板身份检查器吗?
【解决方案3】:

想法:

  • 检查是否设置了单元重用标识符。
  • 检查您是否在情节提要中正确设置了集合视图子类。
  • 将print 语句放入您的cellForItemAtIndexPath 函数中,看看它是否被调用过。
  • 不确定这是否应该是一个问题,但Array 实际上是 Swift 中的一种类型。使用它作为变量名可能会以某种方式混淆逻辑。将其重命名为 array(小写)。无论如何,我相信这是变量名的最佳做法。
  • 对cellForItemAtIndexPath中的单元格做些别的事情,比如改变背景颜色。如果这样可行,那么您对标签所做的操作可能有问题。

【讨论】:

    【解决方案4】:

    你的问题出在

    func collectionView(collectionView: UICollectionView, numberOfItemsSection section: Int)

    方法签名。这是错误的。应该是

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int

    并且使用override 规范(因为UICollectionViewController 有自己的,这就是为什么你得到空集合的原因)。

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
      return Array.count
    }
    

    【讨论】:

      【解决方案5】:

      首先检查你是否使用过这个方法:-

      override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
      return searches.count
      }
      

      每个部分有一个搜索,因此部分的数量是搜索数组的计数。所以我们也可以根据需要使用这个方法。默认情况下我们可以使用retuen 1

      并遵循链接中的步骤以获得更好的解决方案 Check this link

      【讨论】:

      • 协议的默认实现返回 1 所以不可能是它。
      • 哦,是的 @TomaszBąk 每个部分有一个搜索,因此部分的数量是搜索数组的计数。所以我们也可以根据需要使用这种方法。感谢您的指正,因为它有助于其他人更好地理解。
      【解决方案6】:

      在storyboard中选择UIButton,勾选下图中左下角的installed。

      【讨论】:

        【解决方案7】:

        问题在于将标题设置为按钮,请使用以下代码。

        override func
            collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
                var cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! UICollectionViewCell
        
                var button = cell.viewWithTag(1) as! UIButton
                //button.titleLabel?.text = Array[indexPath.row]
                button.setTitle(Array[indexPath.row], forState: UIControlState.Normal)
                return cell
        }
        

        希望对你有帮助

        【讨论】:

          【解决方案8】:

          确保您为单元格设置了正确的重用标识符。

          【讨论】:

            【解决方案9】:

            我遇到了类似的问题,我发现这在某种程度上限制了我的单元格的大小。希望这会有所帮助。

            【讨论】:

              【解决方案10】:

              检查您是否在 StoryBoard 中设置了 CollectionView 出口(数据源、委托),如下所示

              【讨论】:

              • 这是我面临的问题。
              【解决方案11】:

              我从我的 CollectionViewController.m 文件中删除了这段代码

              [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
              

              细胞出现

              【讨论】:

                【解决方案12】:

                检查以确保单元格未被隐藏。在下面添加此代码

                    cell.isHidden = false
                

                【讨论】:

                  【解决方案13】:

                  单元格中的项目没有显示给我,因为我在以编程方式添加子视图时没有为每个添加的子视图设置 tamic。

                  subview.translatesAutoresizingMaskIntoConstraints = false
                  

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2019-12-04
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多