0

Trying to use a custom cell which a label and two buttons. I managed to create a subclass for custom cell and I need to show them in the cell as well. This is my code:

import UIKit
import Alamofire
import SwiftyJSON

class TableViewController: UITableViewController {

@IBOutlet var table: UITableView!

var fruits = ["Orange", "Apple", "Banana"]

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.delegate = self
    self.tableView.dataSource = self
    tableView.rowHeight = 80

    self.tableView.register(tableDetailsVCTableViewCell.self, forCellReuseIdentifier: "cell")
}

@objc func downloadPressed() {
    print("Download Button Presses")
}

@objc func previewPressed() {
    print("Preview Button Pressed")
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}


override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return fruits.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! tableDetailsVCTableViewCell
    cell.lable?.text = fruits[indexPath.row]
    cell.dlB?.addTarget(self, action: #selector(downloadPressed), for: .touchUpInside)
    cell.pvB?.addTarget(self, action: #selector(previewPressed), for: .touchUpInside)

    return cell
}
}

I run the app but nothing shows up. No text in the label nor any buttons

8
  • As you are using storyboard, try commenting this line out self.tableView.register(tableDetailsVCTableViewCell.self, forCellReuseIdentifier: "cell") Commented Jul 23, 2017 at 6:55
  • Are you using a storyboard or UITableViewCell xib? Commented Jul 23, 2017 at 6:56
  • do you mean you see cells but no labels and buttons or you don't see cell at all? Commented Jul 23, 2017 at 6:56
  • @Imad a storyboard Commented Jul 23, 2017 at 6:57
  • @VahidGR Then no need to registerCell in viewDidLoad, Just make sure that you're using same Table view cell identifier. Commented Jul 23, 2017 at 6:59

1 Answer 1

1

As you said, you're using storyboard for designing the Custom TableViewCell then no need to register cell, just remove this line of code from viewDidLoad:

self.tableView.register(tableDetailsVCTableViewCell.self, forCellReuseIdentifier: "cell")

viewDidLoad looks like this:

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.delegate = self
    self.tableView.dataSource = self
    tableView.rowHeight = 80
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.