嗨,我在cellForItemAt中遇到问题,其错误如下。我不明白为什么我无法投射细胞。它发生在else语句的第一行。
列出的UIView的UIConstraintBasedLayoutDebugging类别中的方法也可能会有所帮助。
无法将类型'Spoexs.MenuCell2'(0x10d059660)的值强制转换为'Spoexs.MenuCell1'(0x10d0595c0)。
导入UIKit
导入MIBadgeButton_Swift
class SecondMenuBar:UIView,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {
懒惰的var collectionView:UICollectionView = {
让layout = 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”
let menuItems = [“库存”,“结帐”,“扫描仪”]
varventoryController:InventoryTabController?
覆盖init(frame:CGRect){//运行时
super.init(frame:框架)
collectionView.register(MenuCell1.self,
forCellWithReuseIdentifier:cellId)//注册超类
collectionView.register(MenuCell2.self,
forCellWithReuseIdentifier:cellId)//注册超类
addSubview(collectionView)
addConstraintsWithFormat(“ H:| [v0] |”,views:collectionView)
addConstraintsWithFormat(“ V:| [v0] |”,views:collectionView)
让selectedIndexPath = NSIndexPath(item:0,section:0)
collectionView.selectItem(at:selectedIndexPath as IndexPath,动画:false,scrollPosition:[])//运行时主页按钮发光
//设置单杠
Slider()
NotificationCenter.default.addObserver(forName:.arrayValueChanged,object:nil,queue:OperationQueue.main){[弱自我](notif)在
徽章=“ \(checkout.count)”
自我?.collectionView.reloadData()
}
}
deinit {
NotificationCenter.default.removeObserver(个体)
}
var horizontalBarLeftAnchorConstraint:NSLayoutConstraint?
func slide(){
让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(等于:self.widthAnchor,乘数:1/3).isActive = true
horizontalBarView.heightAnchor.constraint(equalToConstant:3).isActive = true
}
func collectionView(_ collectionView:UICollectionView,didSelectItemAt indexPath:IndexPath){
库存控制器?.scrollToMenuIndex(菜单索引:indexPath.item)
打印(“在这里\(indexPath.section)”)
打印(“在这里\(indexPath.item)”)
}
func collectionView(_ collectionView:UICollectionView,layout collectionViewLayout:UICollectionViewLayout,sizeForItemAt indexPath:IndexPath)-> CGSize {
返回CGSize(width:frame.width / 3,height:frame.height)
}
func collectionView(_ collectionView:UICollectionView,layout collectionViewLayout:UICollectionViewLayout,minimumInteritemSpacingForSectionAt部分: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)为!MenuCell2
cell.imageView2.setImage(UIImage(named:menuItems [indexPath.section])?. withRenderingMode(.alwaysTemplate),用于:.normal)
cell.imageView2.badgeString =徽章
cell.tintColor = UIColor.rgb(r:255,g:0,b:255)//设置色彩颜色
返回单元
}其他{
让cell = collectionView.dequeueReusableCell(withReuseIdentifier:cellId,for:indexPath)为!MenuCell1
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 :)尚未实现”)
}
}
class MenuCell1:UICollectionViewCell {
覆盖init(frame:CGRect){
super.init(frame:框架)
setupViews()
}
需要初始化吗?(编码器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的色彩
返回iv
}()
覆盖var isSelected:布尔{
didSet {
imageView1.tintColor = isSelected?UIColor.rgb(r:76,g:200,b:120):UIColor.rgb(r:190,g:190,b:190)//选择为绿色否则为灰色
}
}
func setupViews(){
addSubview(imageView1)
addConstraintsWithFormat(“ H:[v0(28)]”,视图:imageView1)
addConstraintsWithFormat(“ V:[v0(28)]”,views:imageView1)
addConstraint(NSLayoutConstraint(item:imageView1,attribute:.centerX,relatedBy:.equal,toItem:self,attribute:.centerX,multiplier:1,constant:0))//中心菜单图标
addConstraint(NSLayoutConstraint(item:imageView1,attribute:.centerY,relatedBy:.equal,toItem:self,attribute:.centerY,乘数:1,常量:0))
}
}
class MenuCell2:UICollectionViewCell {
覆盖init(frame:CGRect){
super.init(frame:框架)
setupViews()
}
需要初始化吗?(编码器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:布尔{
didSet {
imageView2.tintColor = isSelected?UIColor.rgb(r:76,g:200,b:120):UIColor.rgb(r:190,g:190,b:190)//选择为绿色否则为灰色
}
}
func setupViews(){
addSubview(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(item:imageView2,attribute:.centerY,relatedBy:.equal,toItem:self,attribute:.centerY,乘数:1,常数:0))
}
}
您注册单元格类的代码是错误的:
collectionView.register(MenuCell1.self, forCellWithReuseIdentifier: cellId)
collectionView.register(MenuCell2.self, forCellWithReuseIdentifier: cellId) // <-- probably not intended
Run Code Online (Sandbox Code Playgroud)
您要同时注册两个类cellId,一个应该是cellId2:
collectionView.register(MenuCell1.self, forCellWithReuseIdentifier: cellId)
collectionView.register(MenuCell2.self, forCellWithReuseIdentifier: cellId2)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
362 次 |
| 最近记录: |