即使定义了协议和委托,其内容视图也为空

问题描述 投票:0回答:1

下面是包含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

            }
        }

    }

}

如果此代码流有任何问题,AnyOne可以仔细检查一下并纠正我。.肯定有问题。

ios swift xcode uicollectionview
1个回答
0
投票

让我做一个例子,假设您正在使用原型单元并在代码上注册,您将不会使用相同的Class来构造它们。原型将是没有IBOutlet的类(来自sotryboards):

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

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

enter image description here

enter image description here

enter image description here

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上使用或不使用这些单元格。希望我能帮助:)

© www.soinside.com 2019 - 2024. All rights reserved.