Reputation: 105
I want to add table view inside my xib file but i got error. My controller is
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
}
}
and my Xib file is
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]
}
}
and the error is
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key contentView.'
Upvotes: 0
Views: 55
Reputation: 349
As per the understanding : You are assigning wrong class as file owner to xib check whether correct classes are assigned and all outlets are connected and circle of ib outlet is filled and it will work fine
Upvotes: 0