【问题标题】:collection view with empty content, even if protocols and delegates are defined包含空内容的集合视图,即使定义了协议和委托
【发布时间】:2020-06-05 03:45:28
【问题描述】:

下面是包含 CollectionView 及其属性的 Triggered viewController:

 import UIKit

struct CellModel {
    let imgImage: UIImage
    let carNumber: String
    let locationInfo: String
    let modifiedDate: String
    let modifiedBy: String

}

class MerchandizeEntranceViewController: UIViewController {


    let data: [CellModel] = [CellModel(imgImage: UIImage(imageLiteralResourceName: "car"), carNumber: "11, 23, 34", locationInfo: "Seoul, South Korea", modifiedDate: "12-01-2020 at 6:30pm", modifiedBy: "Mr.Kim"),CellModel(imgImage: UIImage(imageLiteralResourceName: "car"), carNumber: "11, 23, 34", locationInfo: "Seoul, South Korea", modifiedDate: "12-01-2020 at 6:30pm", modifiedBy: "Mr.Kim"),CellModel(imgImage: UIImage(imageLiteralResourceName: "car"), carNumber: "11, 23, 34", locationInfo: "Seoul, South Korea", modifiedDate: "12-01-2020 at 6:30pm", modifiedBy: "Mr.Kim"),CellModel(imgImage: UIImage(imageLiteralResourceName: "car"), carNumber: "11, 23, 34", locationInfo: "Seoul, South Korea", modifiedDate: "12-01-2020 at 6:30pm", modifiedBy: "Mr.Kim"),CellModel(imgImage: UIImage(imageLiteralResourceName: "car"), carNumber: "11, 23, 34", locationInfo: "Seoul, South Korea", modifiedDate: "12-01-2020 at 6:30pm", modifiedBy: "Mr.Kim"),CellModel(imgImage: UIImage(imageLiteralResourceName: "car"), carNumber: "11, 23, 34", locationInfo: "Seoul, South Korea", modifiedDate: "12-01-2020 at 6:30pm", modifiedBy: "Mr.Kim"),CellModel(imgImage: UIImage(imageLiteralResourceName: "car"), carNumber: "11, 23, 34", locationInfo: "Seoul, South Korea", modifiedDate: "12-01-2020 at 6:30pm", modifiedBy: "Mr.Kim"),CellModel(imgImage: UIImage(imageLiteralResourceName: "car"), carNumber: "11, 23, 34", locationInfo: "Seoul, South Korea", modifiedDate: "12-01-2020 at 6:30pm", modifiedBy: "Mr.Kim")]



    let idCell = "idCell"
    @IBOutlet weak var customCollectionView: UICollectionView!
    @IBOutlet weak var addButton: UIButton!



    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = .cyan
        customCollectionView?.dataSource = self
        customCollectionView?.delegate = self
       customCollectionView?.register(CustomCell.self, forCellWithReuseIdentifier: idCell)





    }

}





class CustomCell: UICollectionViewCell {

    @IBOutlet weak var imgImage: UIImageView!
    @IBOutlet weak var carNumber: UILabel!
    @IBOutlet weak var locationInfo: UILabel!
    @IBOutlet weak var modifiedDate: UILabel!
    @IBOutlet weak var modifiedBy: UILabel!


    public func configModel(with model: CellModel ) {

        imgImage?.image = model.imgImage
        carNumber?.text = model.carNumber
        locationInfo?.text = model.locationInfo
        modifiedDate?.text = model.modifiedDate
        modifiedBy?.text = model.modifiedBy
    }

}

extension MerchandizeEntranceViewController: UICollectionViewDelegate {

}

extension MerchandizeEntranceViewController: UICollectionViewDelegateFlowLayout {
    private func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        return CGSize(width: 384, height: 100)

    }
}

extension MerchandizeEntranceViewController: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return data.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: idCell, for: indexPath) as! CustomCell
        cell.configModel(with: data[indexPath.row])

        return cell
    }


}

下面是“didSelectItemAt indexpath:”在我的 mainViewController 中调用的方法,它触发了DetailedViewcontrolers(上面的 ViewController 内容)显示了它们的内容:

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if let cell_Menu = collectionView.dequeueReusableCell(withReuseIdentifier: "menuCell", for: indexPath) as? MenuCell {
        switch indexPath.row {
        case 0:
            self.performSegue(withIdentifier: "segue0", sender: AnyClass.self)
        case 1:
            self.performSegue(withIdentifier: "segue1", sender: AnyClass.self)
        case 2:
            self.performSegue(withIdentifier: "segue2", sender: AnyClass.self)
        case 3:
            self.performSegue(withIdentifier: "segue3", sender: AnyClass.self)
        case 4:
            self.performSegue(withIdentifier: "segue4", sender: AnyClass.self)
        case 5:
            self.performSegue(withIdentifier: "segue5", sender: AnyClass.self)
        case 6:
            self.performSegue(withIdentifier: "segue6", sender: AnyClass.self)


        default: NSLog("no view is defined for this cell")

        }
    } else {
        if let cell_Story = collectionView.dequeueReusableCell(withReuseIdentifier: "storyCell", for: indexPath) as? StoryCell {
            switch indexPath.row {
            case 0:
                NSLog("No addidtional view defined for story cell yet.. .")
            default: break

            }
        }

    }

}

如果这段代码流有任何问题,任何人都可以查看并纠正我..肯定有问题..

【问题讨论】:

  • 您检查过您的 IBOutlet 吗?有没有 UICollectionView 的引用
  • 当然,我做到了……它有参考……所有连接..
  • 首先注册 1. customCollectionView?.register(CustomCell.self, forCellWithReuseIdentifier: idCell) 2. customCollectionView?.dataSource = self customCollectionView?.delegate = self 3. customCollectionView?.reloadData() //虽然您的情况不需要它
  • 不相关但将cellForRowAt 之外的单元格出列是没有意义的,特别是该单元格无论如何都未使用。奇怪的参数AnyClass.self 是什么?如果您使用的是原型单元格,则不得注册这些单元格。
  • @vadian,大声笑.. 是的,我知道这很奇怪,但其他集合 View 的单元格也被按下并触发到同一个 viewController,这使它看起来更奇怪.. 可以说,我得到了2个具有不同单元格数量的不同集合视图,如果我不使用“出队”两个集合视图中的两个单元格,即使它们各自具有不同的内容,它们也会继续显示相同的内容..这就是为什么我暂时将其保留在那里以首先检查是否意见正在发挥作用...... )

标签: ios swift xcode uicollectionview


【解决方案1】:

让我举个例子,假设您正在使用原型单元并在代码上注册,您不会使用相同的类来构造它们。已注册单元的类将没有任何 IBOutlet(没有来自情节提要):

let regCell = "regCell"
...
customCollectionView.register(RegCell.self, forCellWithReuseIdentifier: regCell)
...
class RegCell: UICollectionViewCell {
    var textField = UITextField()

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

        contentView.addSubview(textField)
        textField.translatesAutoresizingMaskIntoConstraints = false

        NSLayoutConstraint.activate([
            ...
        ])
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

而且,在同一个项目中,您决定使用原型单元:

class ProCell: UICollectionViewCell {
    @IBOutlet var label: UILabel!
}

要处理这两种不同类型的单元实现,您的cellForItemAt 可以是:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    if indexPath.row % 2 != 0 {
        // Setting Registered cells
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: regCell, for: indexPath) as! RegCell
        cell.textField.placeholder = "Registered Cell"

        return cell
    } else {
        // Setting Prototypes cells
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: proCell, for: indexPath) as! ProCell
        cell.backgroundColor = .gray
        cell.label.text = "Prototype"

        return cell
    }
}

这只是一个实现示例,您可以在同一个 collectionView 上使用这些单元格,也可以不使用。希望我能帮上忙:)

【讨论】:

  • 他们做同样的事情:)
  • 如果你使用 register 你不使用原型,它是一个或另一个。如果您选择使用注册,您需要摆脱单元格的出口并在代码上设置所有项目(UILabel、UIImages 等)约束。
  • @paulRick,所以你的意思让我有点困惑,很抱歉这么说。你能用一个例子来澄清一下吗..?我很感谢你的评论...谢谢..)
  • CollectionView 正在加载到视图中,但未显示单元格.. /
  • @O_S paulRick 用截图编辑了答案。它适用于您的情况。在单元格中,项目应该有要显示的约束。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多