0

I'm doing with Xcode 10. I have a tableview in ViewController.

The cell has label which is set as leading, tralling, top and bottom, line is 0

In ViewDidload() I added

override func viewDidLoad() {
        super.viewDidLoad()
tableview.rowheight = UITableView.automaticDimension
tableview.estimateheight = 98
tableview.reloaddata()
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "CommentCell", for: indexPath) as! CommentCell
        let  idcmt: String
        idcmt = self.CommentIDArray[indexPath.row]
        ref.child("Post_theme/\(userid!)/\(id)/comment/\(idcmt)").observe( .value, with: {(snapshot) in
            let value = snapshot.value as? NSDictionary

            cell.Comment.text = value!["content"] as? String
})

// I have a button in the custom cell but tableview load height wrong even I did not click the button on cell 

self.ref.child("Post_theme/\(self.userid!)/\(self.id)/comment/\(idcmt)/likecomment").observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.hasChild(self.userid!){
 cell.Heartcommentaction = {
                    cell.HeartCommentbutton.tintColor = .lightGray
                    self.TableCommentView.reloadData()
                }
}
})
return cell
}


class CommentCell: UITableViewCell{
var Heartcommentaction : (() -> ()) = {}
@IBOutlet weak var Comment: UILabel!
@IBOutlet weak var HeartCommentbutton: UIButton!
@IBAction func HeartCommentaction(_ sender: Any) {
        Heartcommentaction()
    }
}

But when I click to Viewcontroller, tableview did not load the right height cell ( cell which is needed to autorize is not, cell which is not needed is resize)

Then I add this code

override func viewDidAppear(_ animated: Bool) {
       tableview.reloadData()
  }

the code run well just for initial few cells, but when I scroll down more (I have around over 100 cells), it's wrong height again, when I scroll up the initial cells get again wrong

I looked many solutions but not working for me

Really need help! thank you

UPDATE

this is the screenshot of wrong height cell, some timewrong, sometime right

enter image description here

Harryng
  • 13
  • 6
  • 2
    Share the code for your cell. Also, you don't need to call layoutIfNeeded or setNeedsLayout as long as your cells use autoLayout. – Rob C Apr 02 '20 at 10:20
  • Does this answer your question? [Wrong UITableViewCell height for some cells at first loading](https://stackoverflow.com/questions/32688709/wrong-uitableviewcell-height-for-some-cells-at-first-loading) – Nazir Apr 02 '20 at 10:33
  • @Nazir No, unfortunately – Harryng Apr 02 '20 at 10:42
  • @Rob Code is up. please check it out. thanks – Harryng Apr 02 '20 at 10:42
  • It's hard to say without knowing what the constraints for the button are. Can you post any pictures of the cells that look right and the cells that don't? Do all cells have a text value? Since you are using asynchronous calls in your `cellForRowAt` that may be a problem. Also, each time that `cellForRowAt` you want to make sure that you set all properties of the cell all the time. Not just inside your `if` statements` Lastly, make sure you call super.viewWillAppear in your viewWillAppear method. – Rob C Apr 02 '20 at 11:44
  • @Rob I did try to add tableview.reloaddata() in cellForRowAt, it shows the right height, but get lag when scrolling (any suggestion with lagging?). Please take a look screenshot. I am sure that I did make constraints well, because I tried many times. – Harryng Apr 02 '20 at 12:16

2 Answers2

1

Use :

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 98
}

Also remove tableview.estimateheight = 98

Picode
  • 1,140
  • 1
  • 6
  • 12
0

Give this a shot. My code here may not compile because I don't have your full source code. Remember, cellForRow should synchronously set all values on your cell. And you don't want to call reloadData because you will see glitches when scrolling. You want to call reloadRows. See below and see if that helps:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "CommentCell", for: indexPath) as! CommentCell
    let  idcmt: String
    idcmt = self.CommentIDArray[indexPath.row]

    cell.Comment.text = "..."
    cell.Heartcommentaction = {}

    ref.child("Post_theme/\(userid!)/\(id)/comment/\(idcmt)")
        .observe( .value, with: {(snapshot) in
            let value = snapshot.value as? NSDictionary

            if let value = value, let indexPath = tableView.indexPath(for: cell) {
                cell.Comment.text = value!["content"] as? String
                tableView.reloadRows(at: [indexPath], .fade)
            } else {
                print("nope")
            }
        })

    self.ref.child("Post_theme/\(self.userid!)/\(self.id)/comment/\(idcmt)/likecomment")
        .observeSingleEvent(of: .value, with: { (snapshot) in
            if snapshot.hasChild(self.userid!){

                if let indexPath = tableView.indexPath(for: cell) {
                    cell.Heartcommentaction = {
                        cell.HeartCommentbutton.tintColor = .lightGray
                        tableView.reloadRows(at: [indexPath], .fade)
                    }
                } else {
                    print("bad indexPath")
                }

            } else {
                print("no child")
            }
        })

    return cell
}
Rob C
  • 4,877
  • 1
  • 11
  • 24
  • Thanks for the help. unfortunately, putting a reloadrow after cell which makes the cell keep reloading like forever.... – Harryng Apr 02 '20 at 23:17