提问人:Mert Köksal 提问时间:6/25/2022 最后编辑:Mert Köksal 更新时间:6/25/2022 访问量:212
TableView 单元格致命错误:隐式解开 Optional 值时意外发现 nil
TableView Cell Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
问:
我一直在所有项目中使用相同的协议和功能。但是这个给我带来了致命的错误。
这是我的 Nibloadable
protocol NibLoadable: class {
static var nib: UINib { get }
}
extension NibLoadable {
static var nib: UINib {
return UINib(nibName: String(describing: self), bundle: Bundle(for: self))
}
}
这是我的可重复使用
protocol Reusable: class {
static var reuseIdentifier: String { get }
}
extension Reusable {
static var reuseIdentifier: String {
return String(describing: Self.self)
}
}
在 VideoListTableViewCell 中
static let identifier = String(describing: VideoListTableViewCell.self)
extension VideoListTableViewCell: Reusable, NibLoadable { }
在我的VideoViewController中
class VideoViewController: UIViewController, VideoModule.View {
@IBOutlet weak var tableView: UITableView!
var presenter: VideoModule.Presenter!
var videos: [VideoModel]?
override func viewDidLoad() {
super.viewDidLoad()
presenter.fetchVideos()
configureTableView()
}
func configureTableView() {
tableView.register(VideoListTableViewCell.self)
tableView.delegate = self
tableView.dataSource = self
extension VideoViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return configureTableViewCell(indexPath: indexPath)
}
func configureTableViewCell(indexPath: IndexPath) -> VideoListTableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: VideoListTableViewCell.identifier, for: indexPath) as! VideoListTableViewCell
let videoData = (videos?[indexPath.row])!
cell.configure(model: videoData)
return cell
}
因此,在 .register 的 configureTableView 函数中,它会抛出致命错误。我尝试添加标识符来创建我的单元格并注册 tableView,但它也没有解决这个问题。
在 .register 行,我的 tableView 是 tableView UITableView?无 无错误解释。
最后是我的 UITableView 扩展
extension UITableView {
func register<T: UITableViewCell>(_: T.Type) where T: Reusable, T: NibLoadable {
register(T.nib, forCellReuseIdentifier: T.reuseIdentifier)
}
func dequeueReusableCell<T: UITableViewCell>(forIndexPath indexPath: IndexPath) -> T where T: Reusable {
guard let cell = dequeueReusableCell(withIdentifier: T.reuseIdentifier, for: indexPath) as? T else {
fatalError("Could not dequeue cell with identifier \(T.reuseIdentifier)")
}
return cell
}
我试过使用VIPER,所以这是我的VideoContract
protocol VideoModuleViewProtocol: AnyObject {
var presenter: VideoModule.Presenter! { get set }
}
protocol VideoModuleInteractorProtocol: AnyObject {
var presenter: VideoModule.Presenter? { get set }
func fetchVideos()
}
protocol VideoModulePresenterProtocol: AnyObject {
var view: VideoModule.View? { get set }
var interactor: VideoModule.Interactor! { get set }
var router: VideoModule.Router! { get set }
func fetchVideos()
func didFetch(videos: [VideoModel])
}
protocol VideoModuleRouterProtocol: AnyObject {
}
struct VideoModule {
typealias View = VideoModuleViewProtocol
typealias Interactor = VideoModuleInteractorProtocol
typealias Presenter = VideoModulePresenterProtocol
typealias Router = VideoModuleRouterProtocol
}
我已将故事板文件创建为 VideoListTableViewCell.xib,并同时创建了两个 tableviewcell 文件。一切都连接到正确的地方。
我怎样才能克服这个毫无意义的问题?
答: 暂无答案
上一个:如何将按钮的背景图像设置为无
评论
print(tableView)
tableView.register(...)
tableView
nil
VideoViewController
VideoViewController
VideoViewController()
segue
VideoViewController()
viewDidLoad()
VideoViewController()