Xib文件中的TableView

时间:2019-01-31 14:46:10

标签: ios swift uitableview

我想在xib文件中添加表视图,但出现错误。我的控制器是

class ViewController: UIViewControllerUITextViewDelegate,UITableViewDataSource,UITableViewDelegate{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let identifier = "Cell"

        var cell:UITableViewCell? = yerImleriView.tableView.dequeueReusableCell(withIdentifier: identifier)

        if cell == nil {
            _ =  YerImleri().loadNib() as UIView
            tableView.register(UINib(nibName: "YerImleri", bundle: nil), forCellReuseIdentifier: identifier)
            cell = yerImleriView.tableView.dequeueReusableCell(withIdentifier: identifier)
            cell?.textLabel?.textColor = UIColorFromRGB(rgbValue: 0xEFEFEF)
            cell?.textLabel?.text = items[indexPath.row]
            cell?.backgroundColor = UIColorFromRGB(rgbValue: 0x3A4858)
            cell?.textLabel?.font = UIFont (name: "Proxima Nova", size: 16)
        }
        return cell!
    }
    var items: [String] = ["Katmanlar", "Yer Imleri", "GPS Cihazlari", "Kategorik Arama","Cevrimdisi Haritalar"]

    override func viewDidLoad() {
        super.viewDidLoad()

        yerImleriView.tableView.delegate = self
        yerImleriView.tableView.dataSource = self
}
}

我的Xib文件是

class YerImleri: UIView{

    @IBOutlet var contentView: UIView!
    @IBOutlet weak var tableView: UITableView!

    override init(frame: CGRect){
        super.init(frame: frame)
        commonInit()
    }

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

    private func commonInit(){
        Bundle.main.loadNibNamed("YerImleri", owner:self, options: nil)
        addSubview(contentView)
        contentView.frame = self.bounds
        contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
    }
}

,错误是

***由于未捕获的异常'NSUnknownKeyException'而终止应用程序,原因:'[setValue:forUndefinedKey:]:此类与键contentView的键值不兼容。'

1 个答案:

答案 0 :(得分:0)

根据了解:您为xib分配了错误的类作为文件所有者,请检查是否分配了正确的类,并且所有插座均已连接,并且ib插座的圆圈已填充,并且可以正常工作