I have an iOS app where I added a UITableView
to my view in Interface Builder. Then I set up the tableview in the UIViewController
like this:
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 50
tableView.delegate = self
tableView.dataSource = self
tableView.contentInset = UIEdgeInsets(top: 25, left: 0, bottom: 0, right: 0)
And I also implemented the following methods:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("test table")
let course = self.courses[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: CourseTableViewCell.identifier, for: indexPath) as! CourseTableViewCell
cell.make(course, location: self.currentLocation)
return cell
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return courses.count
}
However, the methods are never called, the table view isn't being displayed and I get no errors in my console? Is there something I missed while trying to add this?
Thanks.
As per request in the comments:
courses
array is not empty courses
array is being filled before reloadData()
Try this instead of your code :
@IBOutlet weak var tableView: UITableView!{
didSet {
self.tableView.delegate = self
self.tableView.dataSource = self
}
}
Make sure your controller is conforming to the two protocols: UITableViewDelegate
and UITableViewDataSource
Then add this method to make sure that your cells do not have 0 pts height:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 80 // adjust height of row as needed
}
just try to implements "heightForRowAtIndexPath" I think your table is properly reloaded but due to constraints in cell UITableViewAutomaticDimension is not working. 2 - make sure courses is properly allocated, use Debugger in numberOfRowsAtIndexPath to check items in courses
You should test if the delegate methods are executed by setting a breakpoint in a method like numberOfSections or numberOfRowsInSection.
When the breakpoint doesn't get hit the delegates are not set properly.
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.