Why I've set up a prototype cell but nothing showed up?

48 Views Asked by At
class DemoTableController: UIViewController,UITableViewDataSource,UITableViewDelegate {

@IBOutlet weak var answerView: UITableView!
override func viewDidLoad() {
    super.viewDidLoad()
    answerView.delegate = self
    answerView.dataSource = self
    answerView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "reuseIdentifier")



}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.

    return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return 5
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as! UITableViewCell

    // Configure the cell...

    return cell
}

I add the tableView to a view in a ViewController via StoryBoard.

And then I add a button to my prototype cell. The blank tableView does show up , but it's without the button.

Thank you in advance.

2

There are 2 best solutions below

1
On BEST ANSWER

Delete this line,your code should work.Here you do not need registerClass

 answerView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "reuseIdentifier")
2
On

Please, make sure you correctly set your identifier within your storyboard.

Storyboard

It has to be the same that in your line below :

let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as! UITableViewCell

Also, you probably won't need to use the registerClass method on your tableView (in your viewDidLoad).