我不确定我的UICollectionView在做什么

时间:2019-12-31 03:38:16

标签: swift uicollectionview

这是我的问题。我正在尝试遵循在线指南,了解如何执行此快速项目。

https://www.credera.com/blog/mobile-applications-and-web/building-a-multi-directional-uicollectionview-in-swift/

这似乎对他有用,我在评论中找不到任何相关信息。请帮忙!如果我能要求的不仅仅是一个答案,这样我就可以对以后的问题进行推理,那将不胜感激!

这是我的两个文件:

//  CustomCollectionViewController.swift

import UIKit

let reuseIdentifier = "customCell"

class CustomCollectionViewController: UICollectionViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView?.dataSource = self
        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false
        // Do any additional setup after loading the view.
    }

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

    // MARK: UICollectionViewDataSource
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        //#warning Incomplete method implementation -- Return the number of sections
        return 10
    }


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        //#warning Incomplete method implementation -- Return the number of items in the section
        return 10
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! CustomCollectionViewCell

        // Configure the cell
        cell.label.text = "hi"
        return cell
    }

    // MARK: UICollectionViewDelegate


}

//  CustomCollectionViewCell.swift

import UIKit

@IBDesignable
class CustomCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var label: UILabel!

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }

    func setup() {
        self.layer.borderWidth = 1.0
        self.layer.borderColor = UIColor.black.cgColor
        self.layer.cornerRadius = 5.0
    }
}

这是错误日志,仅供参考,我过滤了一些名称。我认为这说明了拉出超出范围的电池的行为。但是,超出范围的是什么?它不是从数组或字典中提取数据,而是动态创建单元格。

2019-12-30 22:10:46.031790-0500 Project_Name[2813:268267] [MC] Lazy loading NSBundle MobileCoreServices.framework
2019-12-30 22:10:46.032990-0500 Project_Name[2813:268267] [MC] Loaded MobileCoreServices.framework
2019-12-30 22:12:06.928652-0500 Project_Name[2813:268267] <UIView: 0x10170d810; frame = (0 0; 768 1024); autoresize = W+H; layer = <CALayer: 0x1c0038240>>'s window is not equal to <UIAlertController: 0x101845c00>'s view's window!
2019-12-30 22:12:09.797048-0500 Project_Name[2813:268267] *** Assertion failure in -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:notify:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3698.34.4/UICollectionView.m:1980
2019-12-30 22:12:09.798240-0500 Project_Name[2813:268267] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'the collection view's data source did not return a valid cell from -collectionView:cellForItemAtIndexPath: for index path <NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}'
*** First throw call stack:
(0x186687164 0x1858d0528 0x186687038 0x1870217f4 0x1906c80d0 0x18fc9e2ac 0x18fc98f68 0x18fc3c058 0x18a6c9948 0x18a6cdad0 0x18a63a31c 0x18a661b40 0x18fc31720 0x18662ecdc 0x18662c694 0x18662cc50 0x18654cc58 0x1883f6f84 0x18fca2804 0x1006020f8 0x18606c56c)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

2 个答案:

答案 0 :(得分:0)

您有一些从旧的Swift版本中错误地命名的委托函数。请尝试以下操作:

override func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 10
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! CustomCollectionViewCell

    // Configure the cell
    cell.label.text = "hi"
    return cell
}

答案 1 :(得分:0)

从您提到的在线资源来看,我认为您在项目中缺少CustomCollectionViewLayout快速文件。尝试从资源下载项目文件,然后再次检查