gpt4 book ai didi

ios - MVVM UITableViewCell 返回 nil

转载 作者:行者123 更新时间:2023-11-30 12:39:53 24 4
gpt4 key购买 nike

我正在尝试在 UITableViewController 上进行 MVVM 集成,但是我不断收到:

fatal error :在 super.init(style:style,reuseIdentifier:reuseIdentifier) 上解包可选值时意外发现 nil。

怎么会呢?我想这与以下内容有关:

cell.menuItemViewModel = SideMenuViewModel(menuItem: item)

如果我删除这一行就不会出现错误?

在sideMenuController中设置数据源

func setupDatasource() {

let sections = [
SectionModel(model: "menu", items: menuItems)
]

dataSource.configureCell = { (ds, tv, ip, item) in
let cell: MenuCell = tv.dequeueReusableCell(withIdentifier: self.reuseIdentifier, for: ip) as! MenuCell
cell.menuItemViewModel = SideMenuViewModel(menuItem: item)


return cell
}


Observable.just(sections)
.bindTo(tableView.rx.items(dataSource: dataSource))
.addDisposableTo(disposeBag)
}

菜单项模型

struct MenuItem {

var title: String
var image: Asset
var imageSelected: Asset
var controller: UINavigationController

init(title: String, image: Asset, imageSelected: Asset, controller: UINavigationController) {
self.title = title
self.image = image
self.imageSelected = imageSelected
self.controller = controller
}


}

View 模型

struct SideMenuViewModel {

let title: String
let image: UIImage
let imageSelected: UIImage


init(menuItem: MenuItem) {
self.title = menuItem.title
self.image = UIImage(asset: menuItem.image)
self.imageSelected = UIImage(asset: menuItem.imageSelected)
}

}

菜单单元格

class MenuCell: UITableViewCell {

var titleLabel: UILabel!
var iconImageView: UIImageView!

var menuItemViewModel: SideMenuViewModel! {
didSet {
//Set title
self.titleLabel.text = menuItemViewModel.title

//Set image
self.iconImageView.image = menuItemViewModel.image

}
}


override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)


setupUI()
setupConstraints()

}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

func setupUI() {

//Add titleLabel
self.titleLabel = UILabel()
self.addSubview(self.titleLabel)

//Add iconImageView
self.iconImageView = UIImageView()
self.addSubview(self.iconImageView)

//Customize titleLabel
self.titleLabel.font = FontFamily.Avenir.Regular.font(size: 26)
self.titleLabel.textColor = UIColor.white.withAlphaComponent(0.6)

//Customize iconImageView
self.iconImageView.contentMode = .scaleAspectFit

//Customize cell
self.selectionStyle = .none
self.backgroundColor = UIColor.clear
}

func setupConstraints() {

//titleLabel constraints
self.titleLabel.snp.makeConstraints({ make in
make.left.equalTo(self.iconImageView.snp.right).offset(-20)
make.centerY.equalTo(self)
make.height.equalTo(50)
make.width.equalTo(100)
})

//iconImageView constraints
self.iconImageView.snp.makeConstraints({ make in
make.left.equalTo(80)
make.centerY.equalTo(self)
make.height.equalTo(50)
make.width.equalTo(50)
make.right.equalTo(self.titleLabel.snp.left).offset(20)
})

}


}

最佳答案

你注册了手机吗?

tableView.register(MenuCell, forCellReuseIdentifier: self.reuseIdentifier)

如果使用 Storyboard 将类添加到单元格中,则无需注册它,也不应该注册。

此外,当您使用错误的 reuseIdentifier 时,也可能会发生错误。

关于ios - MVVM UITableViewCell 返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42347194/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com