【发布时间】:2017-05-16 15:09:00
【问题描述】:
嗨,我的 cellForItemAt 出现问题,它说下面的错误。我不明白为什么我无法投射细胞。它出现在 else 语句的第一行。
中列出的 UIView 上的 UIConstraintBasedLayoutDebugging 类别中的方法也可能会有所帮助。
导入 UIKit 导入 MIBadgeButton_Swift 类 SecondMenuBar:UIView,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout { 懒惰的 var collectionView: UICollectionView = { 让布局 = UICollectionViewFlowLayout() 让 cv = UICollectionView(frame: .zero, collectionViewLayout: layout) cv.backgroundColor = UIColor.rgb(r:240, g:240, b:240) cv.dataSource = 自我 cv.delegate = 自我 返回简历 }() 让 cellId = "cellId" 让 cellId2 = "cellId2" 让 menuItems = [“库存”,“结帐”,“扫描仪”] var inventoryController: InventoryTabController? override init(frame: CGRect){ //在运行时 超级初始化(帧:帧) collectionView.register(MenuCell1.self, forCellWithReuseIdentifier: cellId) //注册超类 collectionView.register(MenuCell2.self, forCellWithReuseIdentifier: cellId) //注册超类 addSubview(collectionView) addConstraintsWithFormat("H:|[v0]|", 视图:collectionView) addConstraintsWithFormat("V:|[v0]|", 视图:collectionView) 让 selectedIndexPath = NSIndexPath(item:0, section:0) collectionView.selectItem(at: selectedIndexPath as IndexPath, animated: false, scrollPosition: []) //home 按钮在运行时发光 //设置水平条 滑块() NotificationCenter.default.addObserver(forName: .arrayValueChanged, object: nil, queue: OperationQueue.main) { [weak self] (notif) in 徽章 = "\(checkout.count)" self?.collectionView.reloadData() } } 初始化{ NotificationCenter.default.removeObserver(self) } var HorizontalBarLeftAnchorConstraint:NSLayoutConstraint? 功能滑块(){ 让 HorizontalBarView = UIView() 如果 #available(iOS 10.0, *) { HorizontalBarView.backgroundColor = UIColor(displayP3Red:0.29, green:0.78, blue:0.47, alpha:0.2) } 别的 { HorizontalBarView.backgroundColor = UIColor.rgb(r:190, g:190, b:190) } HorizontalBarView.translatesAutoresizingMaskIntoConstraints = false addSubview(horizontalBarView) //添加到层次结构 // 需要 x,y 坐标 HorizontalBarLeftAnchorConstraint = HorizontalBarView.leftAnchor.constraint(equalTo: self.leftAnchor) HorizontalBarLeftAnchorConstraint?.isActive = true HorizontalBarView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true HorizontalBarView.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 1/3).isActive = true HorizontalBarView.heightAnchor.constraint(equalToConstant: 3).isActive = true } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { inventoryController?.scrollToMenuIndex(menuIndex: indexPath.item) print("就在这里\(indexPath.section)") print("就在这里\(indexPath.item)") } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 返回CGSize(宽度:frame.width/3,高度:frame.height) } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { 返回 0 } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection 部分:Int) -> Int { 返回 3 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 如果 indexPath.section == 1{ 让 cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId2, for: indexPath) as!菜单单元格2 cell.imageView2.setImage(UIImage(named:menuItems[indexPath.section])?.withRenderingMode(.alwaysTemplate), for: .normal) cell.imageView2.badgeString = 徽章 cell.tintColor = UIColor.rgb(r:255, g:0, b:255) //设置色调颜色 返回单元格 } 别的{ 让 cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as!菜单单元格1 cell.imageView1.image = UIImage(named: menuItems[indexPath.section])?.withRenderingMode(.alwaysTemplate) cell.tintColor = UIColor.rgb(r:255, g:0, b:255) //设置色调颜色 返回单元格 } } 需要初始化?(编码器 aDecoder:NSCoder){ fatalError("init(coder:) 尚未实现") } } 类 MenuCell1: UICollectionViewCell { 覆盖初始化(帧:CGRect){ 超级初始化(帧:帧) 设置视图() } 需要初始化?(编码器 aDecoder:NSCoder){ fatalError("init(coder:) 尚未实现") } 让 imageView1: UIImageView = { 让 iv = UIImageView() iv.image = UIImage(named: "")?.withRenderingMode(.alwaysTemplate) iv.tintColor = UIColor.rgb(r:144, g:157, b:255)//设置每个菜单图像bg的tint颜色 返回四 }() 覆盖 var isSelected: Bool { 设置{ imageView1.tintColor = isSelected ? UIColor.rgb(r:76, g:200, b:120) : UIColor.rgb(r:190, g:190, b:190) //选择时为绿色,否则为灰色 } } 功能设置视图(){ 添加子视图(imageView1) addConstraintsWithFormat("H:[v0(28)]", 视图: imageView1) addConstraintsWithFormat("V:[v0(28)]", 视图: imageView1) addConstraint(NSLayoutConstraint(item: imageView1, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier:1, constant: 0)) //居中菜单图标 addConstraint(NSLayoutConstraint(项目:imageView1,属性:.centerY,relatedBy:.equal,toItem:self,属性:.centerY,乘数:1,常量:0)) } } 类 MenuCell2:UICollectionViewCell { 覆盖初始化(帧:CGRect){ 超级初始化(帧:帧) 设置视图() } 需要初始化?(编码器 aDecoder:NSCoder){ fatalError("init(coder:) 尚未实现") } 让 imageView2: MIBadgeButton = { 让按钮 = MIBadgeButton() var image = UIImage(named: "home")?.withRenderingMode(.alwaysTemplate) button.setImage(image, for: .normal) button.badgeString = 徽章 button.isUserInteractionEnabled = false button.tintColor = UIColor.rgb(r:190, g:190, b:190) //初始按钮色调灰色 返回按钮 }() 覆盖 var isSelected: Bool { 设置{ imageView2.tintColor = isSelected ? UIColor.rgb(r:76, g:200, b:120) : UIColor.rgb(r:190, g:190, b:190) //选择时为绿色,否则为灰色 } } 功能设置视图(){ 添加子视图(imageView2) addConstraintsWithFormat("H:[v0(28)]", 视图: imageView2) addConstraintsWithFormat("V:[v0(28)]", 视图: imageView2) addConstraint(NSLayoutConstraint(item: imageView2, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier:1, constant: 0)) //居中菜单图标 addConstraint(NSLayoutConstraint(项目:imageView2,属性:.centerY,relatedBy:.equal,toItem:self,属性:.centerY,乘数:1,常数:0)) } }无法将“Spoexs.MenuCell2”(0x10d059660) 类型的值转换为“Spoexs.MenuCell1”(0x10d0595c0)。
【问题讨论】: