0

I have a table with a custom header that I dragged into it. The table view also has a prototype cell. When on a device the table bounces back before reaching the bottom of the content. This problem does not happen without the header. It looks like the table view is able to scroll through about the same amount of content if the header is there or not there (with the header it can scroll through the header and about 2 prototype cells, and without the header it can scroll through about 5 prototype cells). I attached a youtube link because the problem is kind of difficult to describe.

https://www.youtube.com/watch?v=4xaOjcUnFVk&feature=youtu.be

enter image description here


My tableviewdelegate functions

extension ProfileViewController: UITableViewDelegate, UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statuses.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let status = statuses[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileStatusCell") as! ProfileStatusCell
        cell.setup(with: status)
        self.tableView.frame.size = tableView.contentSize
        return cell
    }


}

My ViewController class

class ProfileViewController: UIViewController{

    var uid: String?    //Is for the current user
    var statuses = [Status]()
    var avatar: Avatar?{
        didSet{
            avatarButton.setImage( AvatarImage.newAvatar(values: avatar!.values).image!.withRenderingMode(.alwaysOriginal), for: .normal)
        }
    }
    @IBOutlet weak var tableHeader: UIView!
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var nameLabel: UITextField!
    @IBOutlet weak var bio: UITextView!
    @IBOutlet weak var avatarButton: UIButton!  //Must set the image from another viewController
    @IBOutlet weak var followButton: UIButton!
    @IBOutlet weak var messageButton: UIButton!
    @IBOutlet weak var blockButton: UIButton!
    @IBOutlet weak var reportButton: UIButton!
    @IBOutlet weak var logoutButton: UIButton!

    @IBAction func logout(_ sender: UIButton) {
        FBCoreUser(currentUser.uid!).changeVisibility(to: Visibility.invisible)
        FBUser.signOut(completion: {(_) in return})
        //let start = self.presentingViewController as! Start
        self.dismiss(animated: true, completion: nil)
    }
    @IBAction func blockUser(_ sender: UIButton) {
    }
    @IBAction func reportUser(_ sender: UIButton) {
    }

    override func viewDidLoad(){
        tableView.tableHeaderView = tableHeader

        if uid == nil{  //If it's the page for the current user
            self.avatar = currentUser.avatar
            if currentUser.name == nil{
                FBProfileData(currentUser.uid!).get(dataFor: {(pData) in
                    let data = pData as! ProfileData
                    self.fillProfile(name: data.uName, bio: data.bio, statuses: data.statuses!)
                })
            }else{
                fillProfile(name: currentUser.name!, bio: currentUser.bio!, statuses: currentUser.statuses!)
            }
        }else{
            self.logoutButton.isHidden = true
            self.blockButton.isHidden = false
            self.followButton.isHidden = false
            self.reportButton.isHidden = false
            FBProfileData(uid!).get(dataFor: {(pData) in
                let data = pData as! ProfileData
                self.fillProfile(name: data.uName, bio: data.bio, statuses: data.statuses!)
            })
        }
    }

    func fillProfile(name: String, bio: String, statuses: [Status]){
        self.nameLabel.text = name
        self.bio.text = bio
        self.statuses = statuses
        tableView.reloadData()
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "avatarBuilder"{
            if let vc = segue.destination as? AvatarBuilder {
                vc.avatar = avatar
            }
        }
    }

}

extension ProfileViewController: UITableViewDelegate, UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statuses.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let status = statuses[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileStatusCell") as! ProfileStatusCell
        cell.setup(with: status)
        self.tableView.frame.size = tableView.contentSize
        return cell
    }


}

extension ProfileViewController: UITextViewDelegate, UITextFieldDelegate{

    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        if text == "\n" {
            textView.resignFirstResponder()
            //self.view.endEditing()
        }
        return true
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        self.view.endEditing(true)
        return false
    }

    /* Takes the keyboard of a text field off the screen */
    @objc func closeKeyboard() {
        self.view.endEditing(true)
    }

    /* Closes the keyboard when somewhere else on the screen is touched */
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        closeKeyboard()
    }
}

enter image description here

Sam
  • 1,765
  • 11
  • 82
  • 176
  • @Sammy Well there is no scroll view, I am using tableView.reloadData after I add the data into the tableview. I'm guessing that I'm not adding a header right? I just dragged a view into the table view, above the prototype cell and started putting stuff in it – Sam Nov 19 '19 at 00:48
  • Please show your header code where you add stuff. And can you add a picture of your storyboard that clearly shows the hierarchy of your views? – koen Nov 19 '19 at 01:16
  • @Sammy I'm adding the header in the storyboard. I uploaded my code. – Sam Nov 19 '19 at 01:22
  • 2
    `self.tableView.frame.size = tableView.contentSize` this will probably cause trouble since the function get called frequently, why do you need this if your tableView can be scrolling? – Tj3n Nov 19 '19 at 02:30
  • @Sammy Yeah that was it – Sam Nov 19 '19 at 06:29
  • Glads it help, I'll put it as answer to your question – Tj3n Nov 19 '19 at 06:45

1 Answers1

1

Modifying tableView.contentSize during tableView:cellForRowAt: can cause problem since the function get called everytime new cell gonna appear.

If the tableView is not scrollable and you want it to have the same size as it's contentSize, please check this question

Tj3n
  • 9,837
  • 2
  • 24
  • 35