ios - UITableView 原型(prototype)单元崩溃

标签 ios swift uitableview

当我尝试修改单元格 subview 时,UITableViewCell 因“隐式展开可选值时意外发现 nil”而崩溃。

注册tableView单元格

self.tableView.register(TravelTypeTableViewCell.self, forCellReuseIdentifier: "TravelCell")

tablView方法

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 224
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "TravelCell", for: indexPath) as! TravelTypeTableViewCell
    cell.travelTypeSegment.selectedSegmentIndex = 0
    return cell

}

TableViewcell 类

class TravelTypeTableViewCell: UITableViewCell {
    @IBOutlet var travelTypeSegment: UISegmentedControl!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

Storyboard图像

TableViewCell

enter image description here

最佳答案

评论此内容

self.tableView.register(TravelTypeTableViewCell.self, forCellReuseIdentifier: "TravelCell")

对于原型(prototype)单元,您不应该注册它们,因为这会自动为它们完成,并确保 travelTypeSegment 在 IB 中连接

关于ios - UITableView 原型(prototype)单元崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59649464/

相关文章:

ios - 如何在单元测试中以编程方式点击 UIImageView?

ios - 没有数据可用标签未显示在表格 View 中

ios - UITableViewCell 的背景颜色

iOS - 设置 ImageView 背景颜色和蒙版

iphone - 如何在编译时使用不同的接口(interface)声明而不混淆 Interface Builder

ios - 如何在 iOS 中分享 gif 动画图像

ios - 安装测试版后,Xcode 6 显示 Xcode 7 Beta 错误消息

ios - NSCFData 压缩数据崩溃

ios - CLLocationManager 的 startMonitoringSignificantLocationChanges() 和 startUpdatingLocation() 方法有什么区别?

ios - UIAlertView 不应调用 popViewControllerAnimated