Reputation: 83
i have one problem: my tableView doesn't show any data from realm database. But i guess the problem is in cell.
class ClientViewController: UITableViewController {
@IBOutlet var table: UITableView!
var clientsInfo: Results<Client> {
get {
return realm.objects(Client.self)
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(myCell.self, forCellReuseIdentifier: "cell")
table.delegate = self
table.dataSource = self
table.reloadData()
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: myCell.reuseIdentifier, for: indexPath) as! myCell
let info=clientsInfo[indexPath.row]
cell.todoLabel.text = info.firstName
return cell
}
}
class myCell: UITableViewCell {
@IBOutlet weak var label: UILabel!
static let reuseIdentifier = "cell"
@IBOutlet weak var todoLabel: UILabel!
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
}
Upvotes: 0
Views: 602
Reputation: 324
There is a few things wrong in your code,
First:
You're using a UITableViewController, by default this already has a tableView, so you don't need to declare a new tableView like you did
@IBOutlet var table: UITableView!
Second:
In you viewDidLoad you're registering the cell for the default tableView and using your declared table for dataSource and delegate, this won't work. I recommend you remove the table you create and use the default so your viewDidLoad would look like this:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(myCell.self, forCellReuseIdentifier: "cell")
self.tableView.delegate = self
self.tableView.dataSource = self
}
and Third:
you're missing the implementation of :
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
Upvotes: 2
Reputation: 187
In this code you forgot to implement tableView(numberOfRowsInSection:)
method, so it returns 0 by default and your tableview(cellForRowAt:)
method never called.
AND
Maybe this is not about your question but;
You used UITableView
variable(name is table) in UITableView
. It's unneccessary, because UITableView already has a UITableView(name is tableView). And you'have registered your cell into tableView, but it seems you try to dequeue this cell from table.
Upvotes: 0