首页 文章

当按下单元格中的按钮时,Swift会在其他单元格中复制

提问于
浏览
-1

我从firebese中检索数据并在tableView中显示

在tavleViewCell我有butons:喜欢,unLike

当我按下需要隐藏likeButton并显示不兼容并存储/删除firebase中的数据

现在我创建了metods,当我点击Like - >数据保存到firebase并显示不同按钮 BUT 不同按钮显示在另一个单元格中(不是在所有单元格中 - 例如,如果我点击0 indexPath.row,不像按钮显示在0,6,12,17 ..)当我滚动tableView时可能会发生这种情况

在单元格中我有:

protocol TableThemeQuote {

    func likeTap(_ sender: ThemesQuoteCell)
    func unlikeTap(_ sender: ThemesQuoteCell)
}

class ThemesQuoteCell: UITableViewCell {
 @IBOutlet weak var likeBtn: UIButton!
 @IBOutlet weak var unlikeBtn: UIButton!

 var themeDelegate: TableThemeQuote?
 var index: indexPath?

@IBAction func likeBtn(_ sender: UIButton) {
 sender.tag = index.row
 themeDelegate?.likeTap(self)
}

@IBAction func unLikeBtn(_ sender: UIButton) {
sender.tag = index.row
themeDelegate?.unlikeTap(self)
}
}

在ViewController中:

var userMarks: [String] = [String]()

override func viewDidLoad() {
        super.viewDidLoad()
        likeLike()
}

 func likeLike() {
        let userUID = Auth.auth().currentUser!.uid
        let ref = Database.database().reference().child("users").child("\(userUID)")
        ref.observeSingleEvent(of: .value, with: { (snapshot) in
            if let properties = snapshot.value as? [String : AnyObject] {
                 if let peopleWhoLike = properties["userLikes"] as? [String : AnyObject] {
                    self.likeLikeL.append(peopleWhoLike)
                 for (id,person) in peopleWhoLike {
                self.userLikeCheck.updateValue(person, forKey: id)
                     self.userMarks.append(person as! String)
                }
                }
            }
        })
}

在cellForRowAt indexPath中:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "themesQuoteCell") as! ThemesQuoteCell
        let index = indexPath.row
        cell.index = index
        cell.themeDelegate = self
        let mark = "Темы/\(themeName)/\(quotenumber[index.row])"
        if userMarks.contains(mark){
            if cell.likeBtn.tag == index.row {
                cell.likeBtn.isHidden = true
                cell.unlikeBtn.isHidden = false
                break
             }
        }
        return cell
}

在TableThemeQuote扩展中:

扩展ThemeQuoteVC:TableThemeQuote {

func likeTap(_ sender: ThemesQuoteCell) {
guard let tappedIndexPath = themeQuoteTableView.indexPath(for: sender) else { return }

                let ref = Database.database().reference().child("Темы").child("\(self.themeName)").child("\(quotenumber[tappedIndexPath.row])")
                let keyToPost = ref.childByAutoId().key!
                let updateLikes: [String : Any] = ["peopleWhoLike/\(keyToPost)" : Auth.auth().currentUser!.uid]
                ref.updateChildValues(updateLikes, withCompletionBlock: { (error, reff) in
                    if error == nil {
                        ref.observeSingleEvent(of: .value, with: { (snap) in
                            if let properties = snap.value as? [String : AnyObject] {
                                if let likes = properties["peopleWhoLike"] as? [String : AnyObject] {
                                    let count = likes.count
                                    sender.likeLbl.text = "\(count)"
                                    let update = ["quant" : count]
                                    ref.updateChildValues(update)
                                    sender.likeBtn.isHidden = true
                                    sender.unlikeBtn.isHidden = false
                                    sender.likeBtn.isEnabled = true
                                }
                            }
                        })
                    }
                })
                ref.removeAllObservers()
                }
func unlikeTap(_ sender: ThemesQuoteCell) {

        sender.unlikeBtn.isEnabled = false
        guard let uid = Auth.auth().currentUser?.uid else { return }
        let ref = Database.database().reference().child("users").child("\(uid)")
        guard let tappedIndexPath = themeQuoteTableView.indexPath(for: sender) else { return }
        let mark = "Темы/\(themeName)/\(quotenumber[tappedIndexPath.row])"
        unlikeCellBtn(index: tappedIndexPath.row, sender: sender)
        if sender.unlikeBtn.tag == tappedIndexPath.row {
        ref.observeSingleEvent(of: .value, with: { (snapshot) in
            if let properties = snapshot.value as? [String : AnyObject] {
                if let peopleWhoLike = properties["userLikes"] as? [String : AnyObject] {
                    for (id,person) in peopleWhoLike {
                        if person as! String == mark {
                            ref.child("userLikes").child(id).removeValue(completionBlock: { (error, reff) in
                                if error == nil {
                                    ref.observeSingleEvent(of: .value, with: { (snap) in
                                        if let prop = snap.value as? [String : AnyObject] {
                                            if let likes = prop["userLikes"] as? [String : AnyObject] {
                                                let count = likes.count
                                                ref.updateChildValues(["quantLikes" : count])
                                            }else {
ref.updateChildValues(["quantLikes" : 0])
                                            }
                                        }
                                    })
                                }
                            })
                            sender.likeBtn.isHidden = false
                                                        sender.unlikeBtn.isHidden = true
                                                        sender.unlikeBtn.isEnabled = true
                                                        break
                        }
                    }
                }
            }
        })
        }
        ref.removeAllObservers()
        }

我做错了什么,如何获得当前点击并显示/隐藏我按下的那个按钮?

1 回答

  • 0

    细胞被重复使用 . 这部分代码

    if userMarks.contains(mark){
            if cell.likeBtn.tag == index.row {
                cell.likeBtn.isHidden = true
                cell.unlikeBtn.isHidden = false
                break
             }
        }
    

    根据某些条件设置 hidden 属性,但如果不满足条件,则保持 hidden 状态不变 .

    你要做的是添加一个 else 子句来设置默认值(顺便说一下, break 语句是没有意义的)

    if userMarks.contains(mark) && cell.likeBtn.tag == index.row {
          cell.likeBtn.isHidden = true
          cell.unlikeBtn.isHidden = false     
      } else {
          cell.likeBtn.isHidden = <default value>
          cell.unlikeBtn.isHidden = <default value>
      }
    

相关问题