首页 > 解决方案 > 如何在不同的类中调用 table.reloadData 之前为 Firebase 更新实现完成处理程序?

问题描述

Classes: ViewControllerTableViewCell and homepage ///cell 类用于主页上的单元格

问题:按下按钮会触发 1)更新 firebase 数据库(在 ViewControllerTableViewCell 上)的 IBAction 和 2)通过 table.reloadData 进行 UI 更改的模型(在 ViewControllerTableViewCell 和主页上)。

问题是 reloadData 会导致识别错误的行并在 firebase 中更新错误的行。我通过删除 reloadData 检查了这一点,然后通过 IBAction 在 firebase 中更新了正确的行。大概这是因为 reloadData 在数据库完成之前执行,然后它返回的行是错误的。我只在 Xcode 13 上遇到这个问题。

类 ViewControllerTableViewCell:

@IBAction func buttonPressed(_ sender: UIButton) {
 ...
 **ref.child("users").child(self.postID).updateChildValues(updates)**
 }

@objc func likeButtonTapped() {
    // Calls the corresponding function of the delegate, which is set to the view controller.
    delegate?.didTapLikeButton(on: row, didLike: !model.didLike)
    }

    override func awakeFromNib() {
    like?.addTarget(self, action: #selector(likeButtonTapped), for: .touchUpInside)
    }

   func setup(with model: CellModel) {
   }

班级主页

 //in cellforRowAt {
 cell.setup(with: model)
 }

 // in viewdidLoad {
 setupmodels()
 }

 private func setupModels() {
 ....
 models.append(model)
 }

extension homepage: ViewControllerTableViewCellDelegate {
// Called from the cell's button action through the delegate property.
func didTapLikeButton(on row: Int, didLike: Bool) {
    // Updates the corresponding cell's model so that it will update the cell when
    // the table view reloads with the updated models.
    models[row].didLike = didLike
    **table.reloadData**
    print("BBBBBBBBB")
}

//第一个答案后添加

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? ViewControllerTableViewCell {
        let immy = cell.viewWithTag(1) as? UIImageView
        let like = cell.viewWithTag(3) as? UIButton
        let person: Userx = people[indexPath.row]
        let text = person.Education
        
        cell.lblName.text = person.Education
        cell.lblName.text = text?.uppercased()
        
        let person5 = colorArray[indexPath.row]
        let person6 = colorArray1[indexPath.row]
        let person7 = colorArray2[indexPath.row]
        
        cell.lblName?.layer.masksToBounds = true
        cell.lblName?.backgroundColor = person5
        like?.backgroundColor = person6
        immy?.backgroundColor = person7
        cell.lblName.baselineAdjustment = .alignCenters
        cell.postID = self.people[indexPath.row].postID
        if let PhotoPosts = person.PhotoPosts {
            let url = URL(string: PhotoPosts)
            print(PhotoPosts, "sttdb")
            immy?.sd_setImage(with: url)
        }
        cell.row = indexPath.row
        // Assigns the delegate property to this view controller.
        cell.delegate = self
        cell.delegate2 = self
        let model = models[indexPath.row]
        let modell = modelss[indexPath.row]
        like?.isUserInteractionEnabled = true

        cell.setup(with: model)
        cell.setup1(with: modell)
        
        return cell
    }
    return UITableViewCell()
}

标签: swiftxcodefirebasefirebase-realtime-databasecompletionhandler

解决方案


正如评论中所建议的,

  1. on didTapLikeButton,更新对当前模型的更改。

  2. 而不是tableView.reloadData(),重新加载tableView row

     tableView.reloadRows(at: [IndexPath(row: row, section: 0)], with: .automatic)
    

我正在考虑您在 tableView 中只有 1 个部分,如果没有,则获取该部分或indexPath在上面的代码中传递它。


推荐阅读