ios - 带有标题的 TableView 在触底之前反弹

标签 ios swift xcode uitableview

我有一个表格,其中包含我拖入其中的自定义标题。 TableView 也有一个原型(prototype)单元格。 在设备上时,表格会在到达内容底部之前弹回。没有标题就不会发生这个问题。如果标题存在或不存在,看起来表格 View 能够滚动浏览大约相同数量的内容(使用标题可以滚动标题和大约 2 个原型(prototype)单元格,如果没有标题,它可以滚动大约5 个原型(prototype)电池)。我附上了一个 youtube 链接,因为这个问题有点难以描述。

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

enter image description here


我的 tableviewdelegate 函数

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
    }


}

我的 ViewController 类

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

最佳答案

tableView:cellForRowAt: 期间修改 tableView.contentSize 可能会导致问题,因为每次新单元格出现时都会调用该函数。

如果 tableView 不可滚动,并且您希望它的大小与 contentSize 相同,请检查此 question

关于ios - 带有标题的 TableView 在触底之前反弹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58924673/

相关文章:

ios - 使用相同的 View Controller 进行添加、显示和编辑

swift - 来自公钥字符串的 Swift 4 中的 RSA 加密函数

ios - 如何用唯一名称初始化多个对象

ios - 可能有一个 UINavigationController 用于不填充整个 super UIView 的 UITableView

ios - swift : UITableViewCell size to width of parent UITableView with autolayout enabled

ios - 连接到 iOS 的蓝牙外设在后台不工作

objective-c - 同时禁用多个长按手势

swift - 如何将渐变背景添加到所有其他元素后面的 Swift 播放场景?

swift - 将函数分配给保留闭包的对象时如何捕获值

ios - 使用 UITableView 中单元格中的文本