gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 23:25:05 25 4
gpt4 key购买 nike

我有一个表格,其中包含我拖入其中的自定义标题。 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com