【问题标题】:UITableView load wrong cell height SwiftUITableView加载错误的单元格高度Swift
【发布时间】:2020-07-14 06:51:11
【问题描述】:

我正在使用 Xcode 10。我在 ViewController 中有一个表格视图。

单元格的标签设置为前导、拖曳、顶部和底部,行为0

在 ViewDidload() 中我添加了

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()
    }
}

但是当我点击 Viewcontroller 时,tableview 没有加载正确高度的单元格(需要自动调整的单元格不是,不需要的单元格是调整大小)

然后我添加这段代码

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

代码仅对最初的几个单元格运行良好,但是当我向下滚动更多时(我有大约 100 多个单元格),它的高度再次错误,当我向上滚动初始单元格时再次出错

我查看了很多解决方案,但对我不起作用

真的需要帮助!谢谢你

更新

这是错误高度单元格的截图,有时错误,有时正确

【问题讨论】:

  • 分享您单元格的代码。此外,只要您的单元格使用 autoLayout,您就不需要调用 layoutIfNeeded 或 setNeedsLayout。
  • @Nazir 不,很遗憾
  • @Rob 代码已启动。请检查一下。谢谢
  • 在不知道按钮的约束条件的情况下很难说。你能张贴任何看起来正确的细胞和不正确的细胞的照片吗?所有单元格都有文本值吗?由于您在 cellForRowAt 中使用异步调用,这可能是个问题。此外,每次cellForRowAt 时,您都希望确保始终设置单元格的所有属性。不仅仅是在你的 if 语句中最后,确保你在 viewWillAppear 方法中调用 super.viewWillAppear。

标签: ios swift uitableview


【解决方案1】:

使用:

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

同时删除tableview.estimateheight = 98

【讨论】:

  • 你在哪里加载额外的数据?
  • 你的意思是单元格的数据。我刚刚上传了代码
  • 能不能用代码说清楚。这只能返回 98 高度
  • @Harryng,再检查一次。
【解决方案2】:

试一试。我这里的代码可能无法编译,因为我没有您的完整源代码。请记住,cellForRow 应该同步设置单元格上的所有值。而且你不想打电话给reloadData,因为你会在滚动时看到故障。你想打电话给reloadRows。请参阅下文,看看是否有帮助:

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
}

【讨论】:

  • 感谢您的帮助。不幸的是,在单元格之后放置一个重新加载行,这使得单元格不断重新加载......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-10
  • 2021-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多