如何在不同的 class 中调用 table.reloadData 之前为 Firebase 更新实现完成处理程序?

How to implement a completion handler for Firebase update before table.reloadData is called in a different class?

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

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

问题是 reloadData 导致错误的行被识别并在 firebase 中更新错误的行。我通过删除 reloadData 检查了这一点,然后通过 IBAction 在 firebase 中更新了正确的行。推测这是因为 reloadData 在数据库完成之前执行,然后它 returns 的行是错误的。我只有 Xcode 13 btw.

有这个问题

classViewControllerTableViewCell:

@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) {
   }

class主页

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

根据评论中的建议,

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

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

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

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