一个ViewController中有多个CollectionView

问题描述

我尝试在一个ViewController中显示多个集合视图,但是出现崩溃并显示错误:

Thread 1: Exception: "could not dequeue a view of kind: UICollectionElementKindCell with identifier MainCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard"

我在Storyboard中注册了所有单元格,这是我的ViewController代码:

class ViewController: UIViewController,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout  {

    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var horizontalNumbers: UICollectionView!
    @IBOutlet weak var verticalNumbers: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        collectionView.delegate = self
        collectionView.dataSource = self

        horizontalNumbers.delegate = self
        horizontalNumbers.dataSource = self

        verticalNumbers.delegate = self
        verticalNumbers.dataSource = self
    }

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

        if collectionView == collectionView {
           return 81
        } else {
           return 9
        }
    }

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

        if collectionView == collectionView {
           let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MainCell",for: indexPath) as! CollectionViewCell
           cell.backgroundColor = .systemGreen
           return cell
        } else if collectionView == horizontalNumbers {
           let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Horizontal",for: indexPath) as! HorizontalNumbersCell
           return cell
        } else {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Vertical",for: indexPath) as! VerticalNumbersCell
            return cell
        }
    }
 
}

我检查了所有内容两次,并查找了一些示例代码,但不公开为什么会崩溃。

解决方法

您需要为所有三个collectionView注册单元格。以 const authContext = useContext(AuthContext); const { login,error,isAuth } = authContext; useEffect( () => { if (isAuth) { props.history.push("/"); } if (error) { // access Formik's setValues } },// eslint-disable-next-line [isAuth,props.history] ); <Formik initialValues={{ username: "",email: "",password: "",password2: "",}} validationSchema={Yup.object({ email: Yup.string() .email("Invalid email address.") .required("An email is required."),password: Yup.string().required("A password is required."),})} onSubmit={(submissionData,{ setSubmitting }) => { setSubmitting(true); login(submissionData); setSubmitting(false); }} > {(props) => ( <Form> <MyInput label="Email: " name="email" type="email" placeholder="Enter your email address" /> <MyInput label="Password: " name="password" type="password" placeholder="Enter your password" /> <button type="submit" className="btn"> {props.isSubmitting ? ( <ClipLoader size={16} color={"color"} /> ) : ( "Submit" )} </button> </Form> )} </Formik> 为例。

尝试

collectionView

collectionView.register(UINib(nibName: "CollectionViewCell",bundle: nil),forCellWithReuseIdentifier: "MainCell") // if it is loaded in nib,and nib name is CollectionViewCell

collectionView.register(CollectionViewCell.self,forCellWithReuseIdentifier: "MainCell") // if it is written just in code 中。

viewDidLoad等执行相同的操作。

检查https://developer.apple.com/documentation/uikit/uicollectionview/1618089-register以获得更多详细信息。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...