简体   繁体   中英

Using custom tableViewCells created in an xib in swift

I am trying to create a custom tableview cell in interface builder and use it in a tableview created in swift, the relevant code in the controller holding the tableview is as fallows:

class SearchesMainViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    @IBOutlet var contentTableView: UITableView?

    override func viewDidLoad(){
        super.viewDidLoad()
        self.navigationItem.title = "Search"
        self.contentTableView?.registerNib(UINib(nibName: "SingleSearchTableViewCell", bundle: nil), forCellReuseIdentifier: SingleSearchTableViewCellController.reuseID())
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        var cell = tableView.dequeueReusableCellWithIdentifier(SingleSearchTableViewCellController.reuseID(), forIndexPath: indexPath) as SingleSearchTableViewCellController
        return cell
    }

The Cell itself is very simple, 4 labels and a custom class:

class SingleSearchTableViewCellController : UITableViewCell {

@IBOutlet var dateMonthLable : UILabel?
@IBOutlet var dateDayLable : UILabel?
@IBOutlet var yearLable : UILabel?
@IBOutlet var makeModelLable : UILabel?

var search : Search

class func reuseID() -> String{
    return "SingleSearchTableViewCellType"
}

override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
    search = Search()
    super.init(style: style, reuseIdentifier: reuseIdentifier)
}

override func awakeFromNib() {
    super.awakeFromNib()
}

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

}
required init(coder aDecoder: NSCoder) {
    search = Search()
    super.init()
}

init(previousSearch s : Search){
    search = s
    super.init()
}

override init(frame: CGRect) {
    search = Search()
    super.init(frame: frame)
}

If I did not put enough data please feel free to ask and I thank you for your help in advance. Info on swift interacting with xib does not seem to be widely available yet.

EDIT I have removed my custom class and the bindings for my cell look like this: 在此处输入图片说明 after making these changes the error reads as fallows: Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key dateDayLable.'

now it looks like it is trying to bind my views to some view that is not my customUITableViewCellController

When I run into these kinds of issues it usually is caused by two things. One is that the main view in the xib file is a UIView instead of a UITableViewCell . The other is that the custom class field isn't being set to your custom class.

  1. Check your "Datasource" and "Delegate" connections.
  2. Check that you have added to viewController.
  3. Check that you have properly assigned "dequeueReusableCellWithIdentifier".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM