简体   繁体   中英

UITableView doesn't show any cells

I have a ViewController with a UITableView (and one type of UITableViewCell ) set up through Interface Builder. I hooked that up to some IBOutlets :

@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
    tableView.allowsSelection = false
    tableView.dataSource = self
    tableView.register(CommentTableViewCell.self, forCellReuseIdentifier: CommentTableViewCell.identifier)
    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 150
}

Then I implemented the following methods:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    print(self.comments.count)
    return self.comments.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    print(self.comments[indexPath.row], indexPath.row)
    let cell = tableView.dequeueReusableCell(withIdentifier: CommentTableViewCell.identifier, for: indexPath) as! CommentTableViewCell
    cell.comment = self.comments[indexPath.row]
    print(cell)
    return cell

}

func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
    let comment = comments[indexPath.row]
    return comment.user.isMe ? .delete : .none
}


func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    guard editingStyle == .delete, !isDeleting else {
        return
    }

    isDeleting = true

    let comment = self.comments[indexPath.row]
    comment.delete() { result in
        self.isDeleting = false

        switch result {
        case .success:
            self.comments.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .automatic)
        case .failure:
            self.displayError("Could not delete comment")
        }
    }
}

Now this seems simple and all (print statements for debugging purposes), however, the tableView won't actually display any cells. After tableView.reloadData() is called, the numberOfRowsInSection method returns 2. The cellForRowAt also prints 2 CommentTableViewCell 's.

These are however not displayed. After some testing, I found out that the tableview itself however, is displayed.

Now why is it possible that this happens? I don't think I missed anything. Does anyone have experience with this?

Thanks :-)

Assuming the CommentTableViewCell has this comment label, make sure the vertical constraints for the label are set correctly. The problem seems to be about the incorrect cell height.

As suggested by Reinier Melian, try out fixed heights to see if that works. If not, temporarily change the cell to a UITableViewCell , and set the title label instead and make it work. That way, we can isolate the problem better.

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