按下单元格中的按钮时迅速移动,其他单元格中重复

时间:2018-09-30 15:34:43

标签: ios swift uitableview firebase uicollectionviewcell

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

在tavleViewCell中,我有一些按钮:喜欢,不喜欢

当我按下like时,需要隐藏likeButton并显示AboutButton 并从Firebase存储/删除数据

现在,我创建了方法,然后单击“ Like”->,将数据保存到Firebase并在另一个单元格(不是在所有单元格中,例如,如果我单击0 indexPath.row,则在其他单元格中显示不一样的按钮 BUT ),分别在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 :(得分: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>
  }